Show Menu
Cheatography

Powershell Powershell Powershell

Working with variables

To create a new variable, use an assignment statement to assign a value to the variable.
$MyVar­iable = 1, 2, 3
$Path = "­C:­\Win­dow­s\S­yst­em3­2"
To display the value of a variable, type the variable name, preceded by a dollar sign ($).
$MyVar­iable
1
2
3
To change the value of a variable, assign a new value to the variable.
$MyVar­iable = "The green cat."
$MyVariable
The green cat.
To delete the value of a variable, use the Clear-­Var­iable cmdlet or change the value to $null.
Clear-­Var­iable -Name MyVariable
$MyVariable = $null
To delete the variable, use Remove­-Va­riable or Remove­-Item.
Remove­-Va­riable -Name MyVariable
Remove-Item -Path Variab­le:­\My­Var­iable
To get a list of all the variables in your PowerShell session, type Get-Va­riable.
Variables are useful for storing the results of commands.
$Processes = Get-Process
$Today = (Get-D­ate­).D­ateTime
It is also possible to assign values to multiple variables with one statement.
$a = $b = $c = 0
The next example assigns multiple values to multiple variables.
$i,$j,$k = 10, "­red­", $true
$i,$j = 10, "­red­", $true
# $i is 10, $j is "­red­", $k is True
# $i is 10, $j is [objec­t[]], Length 2

Types of variables

$a = 12
System.Int32
$a = "­Wor­d"
System.String
$a = 12, "­Wor­d"
array of System.Int32, System.String
$a = Get-Ch­ildItem C:\Windows
FileInfo and Direct­oryInfo types
To use cast notation, enter a type name, enclosed in brackets, before the variable name (on the left side of the assignment statem­ent).
[int]$­number = 8

Variable substi­tution in strings

Concat­enation
$name = 'Kevin Marquette'
$message = 'Hello, ' + $name
Variable substi­tution
$first = 'Kevin'
$last = 'Marqu­ette'
$message = "­Hello, $first $last."­

Arrays

To create and initialize an array, assign multiple values to a variable.
$A = 22,5,1­0,8­,12­,9,80
The array sub-ex­pre­ssion operator creates an array from the statements inside it.
@( ... )
$a = @("Hello World")
$p = @(Get-­Process Notepad)
Where-­Object filtering
$data | Where-­Object {$_.Fi­rstName -eq 'Kevin'}
$data | Where FirstName -eq Kevin
Where()
$data.W­he­re(­{$_.Fi­rstName -eq 'Kevin'})
Selects objects or object proper­ties.
Get-Pr­ocess | Select­-Object -Property Proces­sName, Id, WS
 

Hash Tables

To create an empty hashtable in the value of $hash, type:
$hash = @{}
You can also add keys and values to a hashtable when you create it.
$hash = @{ Number = 1; Shape = "­Squ­are­"; Color = "­Blu­e"}
To display a hashtable that's saved in a variable, type the variable name.
$hash
hashtables have Keys and Values proper­ties.
$hash.keys
$hash.values
You can iterate over the keys in a hashtable to process the values in several ways.
foreach ($Key in $hash.K­eys) {
    "The value of '$Key' is: $($has­h[$­Key­])"
}
To add keys and values to a hashtable, use the following command format.
$hash[­"­<ke­y>"] = "­<va­lue­>"
$hash["Time"] = "­Now­"
You can also add keys and values to a hashtable using the Add method of the System.Co­lle­cti­ons.Ha­shtable object.
Add(Key, Value)
$hash.Add("Time", "­Now­")

PSCust­omO­bject

Creating a PSCust­omO­bject
$myObject = [PSCustomObject]@{
 ­ ­ ­ Name = 'Kevin'
    Language = 'Power­Shell'
    State = 'Texas'
}
Converting a hashtable
$myHas­htable = @{
 ­ ­ ­ Name = 'Kevin'
    Language = 'Power­Shell'
    State = 'Texas'
}

$myObject = [pscus­tom­obj­ect­]$m­yHa­shtable
Saving to a file
$myObject | Conver­tTo­-Json -depth 1 | Set-Co­ntent -Path $Path
$myObject = Get-Co­ntent -Path $Path | Conver­tFr­om-Json
Adding properties
$myObject | Add-Member -Membe­rType NotePr­operty -Name 'ID' -Value 'KevinMarquette'
$myObject.ID
Remove properties
$myObj­ect.ps­obj­ect.pr­ope­rti­es.r­em­ove­('ID')
Enumer­ating property names
$myObject | Get-Member -Membe­rType NotePr­operty | Select -Expan­dPr­operty Name
Dynami­cally accessing properties
$myObj­ect.'Name'
Convert PSCust­omO­bject into a hashtable
$hashtable = @{}
foreach( $property in $myobj­ect.ps­obj­ect.pr­ope­rti­es.name )
{
    $hashtable[$property] = $myObj­ect.$p­roperty
}
Testing for properties
if( $null -ne $myObj­ect.ID )
if( $myobj­ect.ps­obj­ect.pr­ope­rti­es.m­at­ch(­'ID­').C­ount )

Functions

A simple function
function Get-Ve­rsion {
 ­ ­ ­  $PSVer­sio­nTa­ble.PS­Version
}
Parameters
function Test-M­rPa­rameter {
     param (
 ­ ­ ­ ­ ­ ­ ­  $Compu­terName
 ­ ­ ­  )
 ­ ­ ­  Write-­Output $Compu­terName
}
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          SolarWinds SWIS API Cheat Sheet
          Windows Terminal Cheat Sheet

          More Cheat Sheets by giangpdh

          T-SQL Cheat Sheet