Blog Post

A Month of PowerShell – Day 2 (Variables and Operators)

,

Welcome to Day 2 of my “A Month of PowerShell” series. This series will use the series landing page on this blog at http://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:

$i = 123
$s = 'Hello '
$s2 = "$s $i"
$s2

 

Results   
Hello  123

 

You can build a long string by concatenation:

$q = "SELECT  TOP (1000)"
$q = $q + "        [BusinessEntityId],"
$q = $q + "        [FirstName],"
$q = $q + "        [LastName]"
$q = $q + "  FROM  [AdventureWorks2012].[dbo].[Person];"

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

$q = @"
SELECT  TOP (1000)
        [BusinessEntityId],
        [FirstName],
        [LastName]
  FROM  [AdventureWorks2012].[dbo].[Person];
"@
$q

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 with 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:

[int]$A = 50

Special Variables

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

Get-Help about_automatic_variablesVariables that store PowerShell state information.
Get-Help about_preference_variablesVariables that customize PowerShell behavior.
Get-Help about_environment_variablesWorking 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.

$A = 5,3,4,2,1                           #specific items
$B = 6..10                               #range, integers only
$C = @("String1", "String2", "String3")  #specific separate string items
$D = @()                                 #empty array
[array] $E = "a;e;i;o;u;y" -split ";"    #specific separate string items

Hash Tables

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

$Z = @{"Colorado" = "Denver"; "Virginia" = "Richmond"; "North Carolina" = "Raleigh"}
$Z.Add("Alaska", "Fairbanks")

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

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating