PowerShell for beginners

What is PowerShell?

In the modern world, everyone is striving towards automation. For an Administrator, performing same repetitive tasks can become monotonous which in turn not only reduces the efficiency but can also leads to errors.

PowerShell is an interactive command line tool through which you can automate such mundane tasks. You can execute programs known as ‘script (saved as .ps1 file)’ which contains various cmdlets for the respective task.

What is a cmdlet?

A cmdlet is simply a command through which you can perform an action. The two most helpful cmdlets that everyone should be aware of are:

  • Get-Command
  • Get-Help

Using the cmdlet ‘Get-Command’, you can find all the available cmdlets even if you do not know the exact cmdlet. For example, you want to restart a service from PowerShell, but you do not know the cmdlet. Although you can assume that it may contain the word ‘service’.

PowerShell for Beginners – An Introduction to the Basics

In the below screenshot, you found all the available cmdlets that contain the word ‘service’ and therefore, found the cmdlet to restart a service.

But how can you make use of this cmdlet? You can find more information about it using the cmdlet ‘Get-Help’.

This provides the basic information about the cmdlet and what all parameters can be used in it.

PowerShell for Beginners – An Introduction to the Basics

If you want to find the MS article about a cmdlet, just add the parameter ‘-Online’ at the end and it will open the MS page in the browser.

PowerShell for Beginners – An Introduction to the Basics

PowerShell vs PowerShell ISE

Now you know how you can execute a program or a cmdlet in PowerShell but where should you write a program. You can do it in PowerShell ISE.

PowerShell for Beginners – An Introduction to the Basics

Variable

Any character that has a symbol ‘$’ in front of it becomes a variable. A variable comes in play when you need to store a value so that you can make use of it later in the script.eg

You have 2 values ‘10’ and ‘4’ and you must perform 3 functions on it, like addition, subtraction, multiplication. So, there are 2 ways to perform this task

a) 10+4, 10-4, 10*4

b) $a = 10

$b = 4

$a + $b

$a – $b

$a * $b

PowerShell for Beginners – An Introduction to the Basics

Data Types in PowerShell

There are different data types in PowerShell that include integers, floating-point values, strings, Booleans, and DateTime values which are like what you use in our daily life. The GetType method returns the current data type of the given variable.

Variables may be converted from one type to another by explicit conversion.

  • Integer: It is of the type ‘whole number’.  Any decimal value is rounded off.
  • Floating Point: The value is of decimal type.
  • String: As the name suggests, it is used for storing letters and characters.
  • Boolean: This value can be either True or False. If it is existing, then it is True otherwise it’ll be False.
  • DateTime: As the name suggests, it is of the type of date and time.

Arrays

Arrays are most often used to contain a list of values, such as a list of usernames or cities. PowerShell arrays can be defined by wrapping a list of items in parentheses and prefacing with the ‘@’ symbol. Eg

$nameArray = @(“John”,”Joe”,”Mary”)

Items within an array can be accessed using their numerical index, beginning with 0, within square brackets like so: $nameArray[0]. Eg

$nameArray[0] will be the first value in the array, ie “John”

$nameArray[1] will be the second value in the array, ie “Joe”

In simple terms, an array is like a column in excel containing similar type of data.

Name
John
Joe
Mary

Practice 1: Create arrays 1 and 2 as below. The 3rd array should show the sum of each corresponding value in array 1 and 2.

Array1Array2Array3
145
257
369

Note: Make use of the concept of concatenation. Our focus should be to understand how indexing happens in an array and how you can make use of it to achieve the above task.

Hashtable

A more advanced form of array, known as a hashtable, is assigned with squiggly brackets prefaced by the ‘@’ sign. While arrays are typically (but not always) used to contain similar data, hashtables are better suited for related (rather than similar) data. Individual items within a hashtable are named rather than assigned a numerical index, eg

$user=@{FirstName=”John”; LastName=”Smith”; MiddleInitial=”J”; Age=40}

Items within a hashtable are easily accessed using the variable and the key name. eg

$user.LastName will return the value ”Smith”.

To easily understand hashtables, you can relate it to the following example.

As per the below example, you have a table in which the first column contains as “FirstName”, the second column contains the “LastName”, the third column contains the “MiddleInitial” and the fourth column contains the “Age”.

FirstNameLastNameMiddleInitialAge
JohnSmithD40
JoeParkerL32
GarySmithN25

Now if you store the above table in a hashtable “$user”, then in order to call the first column you would have to run the command “$user.FirstName” and it will list out all the first names in column 1.

Practice 2: Create 2 hashtables as below and the third hashtable should be calculated as (DaysWorked * SalaryPerDay).

Hashtable 1Hashtable 2Hashtable 3
NameDaysWorkedNameSalaryPerDayNameSalary
John12John100John1200
Joe20Joe120Joe2400
Mary18Mary150Mary2700

About the Author

Alex Rodrick

Alex is an expert in various Messaging technologies like Microsoft Exchange, O365, IronPort, Symantec Messaging Gateway. He is passionate about PowerShell scripting and like to automate day to day activities to improve efficiency and overall productivity.

Comments

  1. Milica

    Can someone explain me how to make Hashtable 3, using Hashtable 1 and 2?
    Thanks.

    1. Timothy

      $hashtable1 = @{
      “key1” = “value1”
      “key2” = “value2”
      }

      $hashtable2 = @{
      “key3” = “value3”
      “key4” = “value4”
      }

      $hashtable3 = @{}
      foreach ($key in $hashtable1.Keys) {
      $hashtable3[$key] = $hashtable1[$key]
      }
      foreach ($key in $hashtable2.Keys) {
      $hashtable3[$key] = $hashtable2[$key]
      }

      Write-Output $hashtable3

  2. Hammer1985

    Really nice totorial – well done and thanks for sharing

  3. Rafi

    Thanks, Explained well!!
    How can i access more of that lessons?

  4. Matt

    How are you adding multiple values to a hash table?

  5. AlphaGeek

    Love this! Easy to understand. Looking forward to more.

  6. fieldmonkey

    Nice examples for beginners! Thanks for sharing!

Leave a Reply