Welcome to Day 2 of my “A Month of PowerShell” series. This series will use the series landing page on this blog at //blog.waynesheffield.com/wayne/a-month-of-powershell/. Please refer to this page to see all of the posts in this series, and to quickly go to them.

Variables

As you start working with any scripting language, you inevitably end up needing to get some data and store it in a variable for use later on. In PowerShell, the syntax for a variable is the name of that variable preceded by a dollar sign ($). Values are assigned to the variable with the equals sign (=). (Note – don’t confuse the = assignment operator with the –EQ comparison operator!) When the variable is defined and a value is assigned to it, an appropriate .NET data type is created. For instance, $MyVariable = 123 creates the variable $MyVariable with a System.Int32 data type and assigns the value 123 to it.

String variables are assigned by using quotation marks. But does PowerShell use single or double quotation marks? The answer to that question is… Yes – you can use either single or double quotation marks. However, there is a difference in how they operate. Single quotation marks use the literal value inside the quotation marks as the string. Double quotation marks will perform string substitution of other variables in the string. Let’s look at a quick example:

You can build a long string by concatenation:

Or by the use of a “here-string”:

When using a Here-String, the string starts with @” (or @’), and this must end the line that it is on. The string ends with “@ (or ‘@), which must be on a line by itself. What’s really neat about using a Here-String is that PowerShell respects all line breaks, quotation marks (single or double), and white space within the string and maintains that in the variable.

You can assign variables to specific .NET data types by preceding the variable declaration with the data type:

Special Variables

PowerShell has a few variables that are automatically created, as seen in the following table:

Command Description
Get-Help about_automatic_variables Variables that store PowerShell state information.
Get-Help about_preference_variables Variables that customize PowerShell behavior.
Get-Help about_environment_variables Working with Windows environment variables within PowerShell.

Arrays

Arrays are data structures designed to store a collection of items, which can be of the same type or of different types. Items can be assigned to arrays in a few different manners.

Hash Tables

A hash table is simply a Name-Value pair. The following example shows creating and adding to the hash table:

Operators

Yesterday we covered comparison operators. You can find out information on all of the operators that are available in PowerShell with the following (be sure to investigate any topics referenced in these):

Get-Help about_Operators
Get-Help about_If
Get-Help about_For
Get-Help about_ForEach
Get-Help about_While
Get-Help about_Do
Get-Help about_Switch