We all need to peek at the answers from time to time. This post is simply a quick reference guide for basic PowerShell tips, tricks, how-to’s, explanations, etc.
I’ve found these items useful in the past, and hope you do as well.
Links to Microsoft or other documentation have made where applicable, and I’ll keep adding to it.
Variables
$var = “string”
Assign variable
[Type]$var=”typedVar”
Assign strong typed variable
[ValidateRange(1,9)][int]$x=1
Assign strong typed attribute controlled variable
$a,$b,$c = 0 or $a,$b = ‘a’,’b’
Assign multiple variables
$a,$b = $b,$a
Flip variables
Scopes
global, local, private or script
$global:var = “var”
Assign global scoped variable
Arrays
“a”, “b”, “c”
Array of strings
@()
Empty array
1,(2,3),4
Array within array
,”hi”
Array of one element
$arr[5]
Sixth element of array
$arr[2..20]
Return elements 3 thru 21
$arr[-1]
Return last array element
$arr[-3..-1]
Display last threeelementsofarray
$arr[1,4+6..9]
Elements at index positions 1,4, 6 to 9
@(Get-Process)
Force result to an array
$arr[($arr.length-1)..0]
Reverse array
$arr[1] += 200
Add to existing array item value
$b = $arr[0,1 + 3..6]
New array from elements of $arr array
$z = $arrA + $arrB
Combine two arrays into single array
Arrays
$hash = @{}
Create empty hash table
@{foo=1; bar=’value2′}
Create, initialize hash table
[ordered]@{a=1; b=2; c=3}
Create ordered dictionary
$hash.key1 = 1
Assign 1 to key key1
$hash.key1
Return value of key1
$hash[“key1”]
Return value of key1
$hash.GetEnumerator | sort Key
Sort hash table by Key
[pscustomobject]@{x=1;z=”z”}
Create custom object
Strings
“$var expand”
String with expansion “
‘$var no expand’
String with no expansion ‘
@” Here-String “@
Here-String -quotes, expressions, etc. Single quotes for no expressions
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional
Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.