"Powershell Cheat Sheet"

ADVERTISEMENT
ADVERTISEMENT

Download "Powershell Cheat Sheet"

356 times
Rate (4.8 / 5) 22 votes
PowerShell   C heat   S heet  
PowerShell   C heat   S heet   v 2  
Adapted   w ith   p ermission   f rom   B en   P earce’s   O riginal.  
http://sharepointjack.com/2013/powershell-­‐cheat-­‐sheet-­‐v2-­‐00/  
 
Essential   C ommands  
Setting   S ecurity   P olicy  
 
To   g et   h elp   o n   a ny   c mdlet   u se   g et-­‐help  
View   a nd   c hange   e xecution   p olicy   w ith   G et-­‐
 
Get-Help Get-Service
Execution   a nd   S et-­‐Execution   p olicy  
 
To   g et   a ll   a vailable   c mdlets   u se   g et-­‐command  
Get-Executionpolicy
 
Get-Command
Set-Executionpolicy remotesigned
 
To   g et   a ll   p roperties   a nd   m ethods   f or   a n   o bject   u se   g et-­‐member  
 
Get-Service | Get-Member
 
Functions  
 
Parameters   s eparate   b y   s pace.   R eturn   i s  
 
To   E xecute   S cript  
optional.  
 
function sum ([int]$a,[int]$b)
powershell.exe –noexit &”c:\myscript.ps1”
 
{
 
return $a + $b
 
Variables  
Arrays  
}
 
sum 4 5
Must   s tart   w ith   $  
To   i nitialise  
 
$a = 32
$a = 1,2,4,8
 
Can   b e   t yped  
To   q uery  
True   /   F alse   /   N ull  
 
[int]$a = 32
$b = $a[3]
 
Set   a   V ariable   t o   t rue  
 
 
 
$a = $true
Check   i f   a   V ariable   i s   f alse  
 
Constants  
If ($b –eq $false)
 
Created   w ithout   $  
Is   i t   n ull?  
 
Set-Variable –name b –value 3.142 –option constant
If ($c –eq $null)
 
Referenced   w ith   $  
 
$b
 
 
 
Creating   O bjects  
 
 
To   c reate   a n   i nstance   o f   a   c om   o bject  
 
New-­‐Object   -­‐ comobject   < ProgID>  
 
$a = New-Object –comobject "wscript.network"
 
$a.username
 
To   c reate   a n   i nstance   o f   a   . Net   F ramework   o bject.     P arameters   c an   b e   p assed   i f   r equired  
 
New-­‐Object   – type   < .Net   O bject>  
 
$d = New-Object -Type System.DateTime 2006,12,25
 
$d.get_DayOfWeek()
 
 
 
Writing   t o   C onsole  
Capture   U ser   I nput  
 
Variable   N ame  
Use   R ead-­‐Host   t o   g et   u ser   i nput  
 
$a
$a = Read-Host “Enter your name”
 
or  
Write-Host "Hello" $a
 
Write-Host $a –foregroundcolor “green”
 
 
Miscellaneous  
 
Line   B reak   `  
 
Passing   C ommand   L ine   A rguments  
Get-Process | Select-Object `
 
name, ID
Passed   t o   s cript   w ith   s paces  
 
Comments   #  
myscript.ps1 server1 benp
 
# code here not executed
Accessed   i n   s cript   b y   $ args   a rray  
Merging   l ines   ;  
$servername = $args[0]
$a=1;$b=3;$c=9
$username = $args[1]
Pipe   t he   o utput   t o   a nother   c ommand   |  
 
Get-Service | Get-Member
 
PowerShell   C heat   S heet  
PowerShell   C heat   S heet   v 2  
Adapted   w ith   p ermission   f rom   B en   P earce’s   O riginal.  
http://sharepointjack.com/2013/powershell-­‐cheat-­‐sheet-­‐v2-­‐00/  
 
Essential   C ommands  
Setting   S ecurity   P olicy  
 
To   g et   h elp   o n   a ny   c mdlet   u se   g et-­‐help  
View   a nd   c hange   e xecution   p olicy   w ith   G et-­‐
 
Get-Help Get-Service
Execution   a nd   S et-­‐Execution   p olicy  
 
To   g et   a ll   a vailable   c mdlets   u se   g et-­‐command  
Get-Executionpolicy
 
Get-Command
Set-Executionpolicy remotesigned
 
To   g et   a ll   p roperties   a nd   m ethods   f or   a n   o bject   u se   g et-­‐member  
 
Get-Service | Get-Member
 
Functions  
 
Parameters   s eparate   b y   s pace.   R eturn   i s  
 
To   E xecute   S cript  
optional.  
 
function sum ([int]$a,[int]$b)
powershell.exe –noexit &”c:\myscript.ps1”
 
{
 
return $a + $b
 
Variables  
Arrays  
}
 
sum 4 5
Must   s tart   w ith   $  
To   i nitialise  
 
$a = 32
$a = 1,2,4,8
 
Can   b e   t yped  
To   q uery  
True   /   F alse   /   N ull  
 
[int]$a = 32
$b = $a[3]
 
Set   a   V ariable   t o   t rue  
 
 
 
$a = $true
Check   i f   a   V ariable   i s   f alse  
 
Constants  
If ($b –eq $false)
 
Created   w ithout   $  
Is   i t   n ull?  
 
Set-Variable –name b –value 3.142 –option constant
If ($c –eq $null)
 
Referenced   w ith   $  
 
$b
 
 
 
Creating   O bjects  
 
 
To   c reate   a n   i nstance   o f   a   c om   o bject  
 
New-­‐Object   -­‐ comobject   < ProgID>  
 
$a = New-Object –comobject "wscript.network"
 
$a.username
 
To   c reate   a n   i nstance   o f   a   . Net   F ramework   o bject.     P arameters   c an   b e   p assed   i f   r equired  
 
New-­‐Object   – type   < .Net   O bject>  
 
$d = New-Object -Type System.DateTime 2006,12,25
 
$d.get_DayOfWeek()
 
 
 
Writing   t o   C onsole  
Capture   U ser   I nput  
 
Variable   N ame  
Use   R ead-­‐Host   t o   g et   u ser   i nput  
 
$a
$a = Read-Host “Enter your name”
 
or  
Write-Host "Hello" $a
 
Write-Host $a –foregroundcolor “green”
 
 
Miscellaneous  
 
Line   B reak   `  
 
Passing   C ommand   L ine   A rguments  
Get-Process | Select-Object `
 
name, ID
Passed   t o   s cript   w ith   s paces  
 
Comments   #  
myscript.ps1 server1 benp
 
# code here not executed
Accessed   i n   s cript   b y   $ args   a rray  
Merging   l ines   ;  
$servername = $args[0]
$a=1;$b=3;$c=9
$username = $args[1]
Pipe   t he   o utput   t o   a nother   c ommand   |  
 
Get-Service | Get-Member
 
Do   W hile   L oop  
Do   U ntil   L oop  
 
Can   r epeat   a   s et   o f   c ommands   w hile   a   c ondition   i s   m et  
Can   r epeat   a   s et   o f   c ommands   u ntil   a   c ondition   i s   m et  
 
$a=1  
$a=1  
 
Do   { $a;   $ a++}  
Do   { $a;   $ a++}  
 
While   ( $a   – lt   1 0)  
Until   ( $a   – gt   1 0)  
 
 
For   L oop  
ForEach   -­‐   L oop   T hrough   C ollection   o f   O bjects  
 
Repeat   t he   s ame   s teps   a   s pecific   n umber   o f   t imes  
Loop   t hrough   a   c ollection   o f   o bjects  
 
For ($a=1; $a –le 10; $a++)
Foreach ($i in Get-Childitem c:\windows)
1  
{$a}
{$i.name; $i.creationtime}
 
 
 
 
 
 
 
Switch   S tatement  
If   S tatement  
Another   m ethod   t o   r un   a   s pecific   s et   o f   c ode   g iven  
Run   a   s pecific   s et   o f   c ode   g iven   s pecific   c onditions  
specific   c onditions  
$a = "white"
$a = "red"
if ($a -eq "red")
switch ($a)
{"The colour is red"}
{
elseif ($a -eq "white")
"red" {"The colour is red"}
{"The colour is white"}
"white"{"The colour is white"}
else
default{"Another colour"}
{"Another colour"}
}
Reading   F rom   a   F ile  
Writing   t o   a   S imple   F ile  
Use   G et-­‐Content   t o   c reate   a n   a rray   o f   l ines.     T hen   l oop  
Use   O ut-­‐File   o r   >   f or   a   s imple   t ext   f ile  
through   a rray  
$a = "Hello world"
$a | out-file test.txt
$a = Get-Content "c:\servers.txt"
Or   u se   >   t o   o utput   s cript   r esults   t o   f ile  
foreach ($i in $a)
{$i}
.\test.ps1 > test.txt
Writing   t o   a n   H tml   F ile  
Use   C onvertTo-­‐Html   a nd   >  
$a = Get-Process
$a | Convertto-Html -property Name,Path,Company > test.htm
Writing   t o   a   C SV   F ile  
Use   E xport-­‐Csv   a nd   S elect-­‐Object   t o   f ilter   o utput  
$a = Get-Process
$a| Select-Object Name,Path,Company | Export-Csv -path test.csv
 
Load   a   S nap   I n  
Load   a   S nap   i n   f or   a dded   f unctionality,   s upressing   e rror   i nfo   i f   t he   s nap   i n   i s   a lready   l oaded.  
Add-­‐PSSnapin   m icrosoft.sharepoint.powershell   -­‐ ErrorAction   S ilentlyContinue  
Working   W ith   S hortened   c ommands   ( Aliases)  
Use   G et-­‐Alias   t o   l ist   o ut   a ll   c ommands   w ith   s hortened   a lternatives  
Get-Alias
Find   t he   l ong   f orm   o f   a   c ommand   f rom   i ts   a lias:  
Get-Alias –name dir
Find   a ll   a liases   o f   a   f orm   o f   a   c ommand   f rom   i ts   a lias:  
Get-Alias –Definition “Get-ChildItem”
Refining   o utput  
Where-­‐Object   ( Where)  
Sort-­‐Object   ( Sort)  
Where   i s   u sed   t o   l imit   t he   o utput   o f   a   c ommand  
Limit   w hich   f ields   a re   r eturned  
Command   |   W here   { $_.ParameterName   – like   “ value”}    
Long   F orm:  
$a = dir |Where {$_.PSIsContainer –eq $true}
Dir | Sort-Object Name
Short   F orm:  
Dir | Sort Name, Length
Select-­‐Object   ( Select)  
Listing   D etails  
Limit   w hich   f ields   a re   r eturned  
Sometimes   t here   i s   m ore   t han   i s   s hown   b y   d efault  
Dir | Select Name, Length
Format-­‐List   o utputs   m ore   f ields,   i n   a   l ist   f ormat  
Limit   h ow   m any   r esults   a re   r eturned  
Dir | Format-list
Dir | Select –First 3
Dir | fl
Chaining   M ultiple   C ommands  
Multiple   c ommands   a nd   r efiners   c an   b e   u sed   t o   g et   j ust   t he   r ight   o utput:  
Dir | where {$_.PSIsContainer –eq $true} | Sort name | Select –first 3
 
Learning   a bout   a   r esult   b y   u sing   w here,   t he   d ot   ( .)   a nd   t ab  
Some   c ommands   r eturn   c omplex   r esults   t hat   c an   b e   f urther   b roken   d own.  
It   i s   o ften   h elpful   t o   n arrow   d own   t he   r esults   t o   j ust   o ne   i tem,   a nd   a ssign   t hat   o ne   r esult   t o   a   v ariable   s o  
that   y ou   c an   i nspect   i ts   p roperties.  
$d = Dir #returns too much
$d = Dir | select –first #better, returns one entry
At   t his   p oint   y ou   c an   t ype   $ d.   a nd   h it   t he   t ab   k ey   r epeatedly   t o   s ee   t he   d ifferent   p roperties.  
$d.(tab) #starts to list the properties such as $d.name, $d.fullname
Another   e xample,   u sing   w here   t o   p ick   t he   s pecific   r esult   t o   i nspect  
$d = Dir | Where {$_.name –eq “Windows”}
Page of 3