"Beginning Programming With Powershell Cheat Sheet"

ADVERTISEMENT
ADVERTISEMENT

Download "Beginning Programming With Powershell Cheat Sheet"

343 times
Rate (4.7 / 5) 20 votes
Beginning Programming with Powershell
Cheat Sheet
Variables:
$a
Variable
$a=5
Variable with value
[Int]$a
Typed variable
$a | gm
Determine variable type
$a + $b
Add int variables, or concatenate strings
‘$a’
Returns the literal string “$a”
“$a”
Returns the value held in $a
Pipes and operators:
Dir | gm
Passes the actual directory objects to the “get-member” function
Where-Object{}
Filtering loop operator
?{}
Filtering loop operator (same as Where-Object{})
ForEach-Object{}
Loop operator
%{}
Loop operator (same as ForEach-Object{})
$_
Current object in the pipeline
$_.Name
The name of the current object in the pipeline
-eq
“Equals” comparison operator
-gt
“Greater than” comparison operator
-lt
“Less than” comparison operator
If () {}
If/then operator.
ELSE {}
Try {}
Catch {}
Cmdlets:
Get-Help
Alias help
Where-Object
Alias ?
ForEach-Object
Alias %
Get-Member
Alias gm
Get-Service
Get-Process
Get-ChildItem
Alias dir, also gci
Out-File
Format-Table
Alias ft
Format-List
Alias fl
Test-Path
Beginning Programming with Powershell
Cheat Sheet
Variables:
$a
Variable
$a=5
Variable with value
[Int]$a
Typed variable
$a | gm
Determine variable type
$a + $b
Add int variables, or concatenate strings
‘$a’
Returns the literal string “$a”
“$a”
Returns the value held in $a
Pipes and operators:
Dir | gm
Passes the actual directory objects to the “get-member” function
Where-Object{}
Filtering loop operator
?{}
Filtering loop operator (same as Where-Object{})
ForEach-Object{}
Loop operator
%{}
Loop operator (same as ForEach-Object{})
$_
Current object in the pipeline
$_.Name
The name of the current object in the pipeline
-eq
“Equals” comparison operator
-gt
“Greater than” comparison operator
-lt
“Less than” comparison operator
If () {}
If/then operator.
ELSE {}
Try {}
Catch {}
Cmdlets:
Get-Help
Alias help
Where-Object
Alias ?
ForEach-Object
Alias %
Get-Member
Alias gm
Get-Service
Get-Process
Get-ChildItem
Alias dir, also gci
Out-File
Format-Table
Alias ft
Format-List
Alias fl
Test-Path
More:
-ErrorAction
Determine how errors are handled, e.g. silentlycontinue, Stop, Continue,
Ignore, Inquire
-ErrorVariable
Create an error variable for use later, e.g. -errorvariable +err
$err
Param()
Create parameters, so you can pass values into the script.
Example: param ([string]$FilePath)
function
Create functions to componetize your code.
fname($var) {}
fname $var
Function call.
. .\IncludeFile.ps1;
Dot-source include a file, so it runs as part of your script.
References
Us:
Twitter.com\KenpoDBA
Twitter.com\MidnightDBA
MidnightDBA.com
MidnightSQL.com
MinionWare.net
Powershell documentation on Microsoft.com:
https://msdn.microsoft.com/en-
us/powershell/scripting/powershell-scripting
Page of 2