Show Menu

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 ($).
To change the value of a variable, assign a new value to the variable.
$MyVar­iable = "The green cat."
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
$a = "­Wor­d"
$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

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


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
$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.
hashtables have Keys and Values proper­ties.
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­")


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'
Remove properties
Enumer­ating property names
$myObject | Get-Member -Membe­rType NotePr­operty | Select -Expan­dPr­operty Name
Dynami­cally accessing properties
Convert PSCust­omO­bject into a hashtable
$hashtable = @{}
foreach( $property in $myobj­­obj­­ope­rti­ )
    $hashtable[$property] = $myObj­ect.$p­roperty
Testing for properties
if( $null -ne $myObj­ect.ID )
if( $myobj­­obj­­ope­rti­es.m­at­ch(­'ID­').C­ount )


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


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