Variables

Begin with $:

$a = "Learning Powershell is fun and easy"
$a                           # Outputs the string, syntactic sugar for Write-Host $a
$a.Length
$a.GetType()                 # Everything has a .NET type, in this case String

$a = 5                       # Types are mutable, now Int32
[System.Int32]$myint = 42    # But you can make strongly-typed variables too
[int]$otherint = 24          # Another way to declare an Int32

Comparisons

The <, >, and = symbols are reserved. PS uses -gt, -lt, -eq, etc instead.

  • -eq: Equals
  • -ne: Not equal to
  • -lt: Less than
  • -gt: Greater than
  • -le: Less than or equal to
  • -ge: Greater than or equal to
  • -Like: Matches wildcard pattern
  • -NotLike
  • -Match: Matches regular expression
  • -NotMatch
$var = 42
$var -gt 40    # True
$var -lt 40    # False
$var -eq 42    # True

"Pluralsight" -like "Plural*"         # True
"Pluralsight" -like "?luralsight"     # True
"Pluralsight" -like "Plural*[s-v]"    # True, ends with a char between s and v

"888-368-1240" -match "[0-9]{3}-[0-9]{3}-[0-9]{4}"    # True

Powershell performs implicit type conversion, where the data on the right is converted to the type of the data on the left:

"42" -eq 42    # True, compared as strings
42 -eq "42"     # True, compared as ints
42 -eq "042"    # True
"042" -eq 42    # False

Cmdlets

Everything in Powershell is performed using cmdlets under the hood:

New-Variable -Name var -Value 123    # Long form of $var = 123
Get-Variable var -valueonly          # Output the value of $var
Set-Variable -Name var -Value 789
Clear-Variable -Name var             # Same as $var = $null
Remove-Variable -Name var            # Can no longer access $var

Strings

Can double quote to get quotes in strings:

"I just wanted to say ""Hello World"", OK?"
'I can''t believe how cool Powershell is!'
Escape sequences

Use the backtick:

"Plural`bsight"    # Backspace, outputs 'Plurasight' (doesn't work in ISE for some reason)
"Plural`nsight"    # Newline
"Plural`rsight"    # Carriage return
"Plural`tsight"    # Tab, outputs 'Plural    sight'

Use "here strings" for large blocks of text (can use single or double quotes):

$heretext = @"
This is a really
long block of text
    including some weird whitespace

and blank lines
"@
String interpolation

Only works with double quotes. Can be used inside here strings.

Set-Location C:\PS
Clear-Host
$items = (Get-ChildItem).Count
$loc = Get-Location
"There are $items items in the folder $loc."

# To display the variable name instead of its value, escape with a backtick
"There are `$items items in the folder `$loc."

Can use expressions in strings by wrapping with $():

"There are $((Get-ChildItem).Count) items in the folder $(Get-Location)."
"The 15% tip of a 33.33 dollar bill is $(33.33 * 0.15) dollars"
String formatting
"There are {0} items." -f $items
"There are {0} items in the location {1}." -f $items, $loc
"The 20% tip of a 33.33 dollar bill is {0:0.00} dollars." -f (33.33 * 0.20)

Arrays

Simple arrays
$array = "eggs", "ham"
$array[0]
$array.GetType()                 # Object[]
$array = @("plural", "sight")    # Formal array creation syntax
$array = @()                     # Only way to create an empty array
$array = 1..5                    # [1, 2, 3, 4, 5]

$powersOfTwo = 2, 4, 8, 16, 32
$powersOfTwo -contains 2         # True
$powersOfTwo -notcontains 42     # True

Hash Tables

Simple hash tables
$hash = @{"Key" = "Value";
          "Pluralsight" = "pluralsight.com";
          "Cool guy" = $true}
$hash           # Output all values
$hash["Key"]    # Outputs the value of "Key"
$hash."Key"     # Using object syntax

$mykey = "Pluralsight"
$hash.$mykey       # Outputs 'pluralsight.com'
$hash.$($mykey)    # Evaluating as an expression
$hash.$("Plural" + "sight")

$hash.Remove("Key")
$hash.Contains("Key")                     # False
$hash.ContainsValue("pluralsight.com")    # True
$hash.Keys
$hash.Values

Built-in Variables

$pwd               # Print the working directory
$Home              # User's home directory
$host              # Info about a user's machine
$PID               # Process ID
$PSVersionTable    # Info about current version of Powershell
$_                 # Current Object

results matching ""

    No results matching ""