0% found this document useful (0 votes)
180 views3 pages

PowerShell Cheat Sheet V2.00 PDF

This document provides a cheat sheet for PowerShell commands and concepts. It includes essential commands like Get-Help and Get-Command to get information about cmdlets and commands. It also covers variables, arrays, objects, functions, loops, conditional statements, and more. The cheat sheet is intended to help users learn PowerShell and have concise references for common tasks like getting help, viewing properties of objects, and flow control structures.

Uploaded by

Yoga Koswara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
180 views3 pages

PowerShell Cheat Sheet V2.00 PDF

This document provides a cheat sheet for PowerShell commands and concepts. It includes essential commands like Get-Help and Get-Command to get information about cmdlets and commands. It also covers variables, arrays, objects, functions, loops, conditional statements, and more. The cheat sheet is intended to help users learn PowerShell and have concise references for common tasks like getting help, viewing properties of objects, and flow control structures.

Uploaded by

Yoga Koswara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PowerShell

 Cheat  Sheet  v2  


PowerShell  Cheat  Sheet   Adapted  with  permission  from  Ben  Pearce’s  Original.  
http://sharepointjack.com/2013/powershell-­‐cheat-­‐sheet-­‐v2-­‐00/  
 
Essential  
  Commands   Setting  Security  Policy  
To  get  help     on  any  cmdlet  use  get-­‐help   View  and  change  execution  policy  with  Get-­‐
Get-Help   Get-Service Execution  and  Set-­‐Execution  policy  
To  get  all  a  vailable  cmdlets  use  get-­‐command   Get-Executionpolicy
Get-Command   Set-Executionpolicy remotesigned
To  get  all  p  roperties  and  methods  for  an  object  use  get-­‐member  
Get-Service   | Get-Member
 
Functions  
  Parameters  separate  by  space.  Return  is  
To  Execute    
S cript   optional.  
powershell.exe –noexit &”c:\myscript.ps1” function sum ([int]$a,[int]$b)
 
{
  return $a + $b
 
Variables  
 
Arrays   }
sum 4 5
Must  
  start  with  $   To  initialise  
$a = 32 $a = 1,2,4,8
 
Can  be  typed   To  query   True  /  False  /  Null  
 
[int]$a = 32 $b = $a[3]
  Set  a  Variable  to  true  
    $a = $true
 
  Check  if  a  Variable  is  false  
Constants  
  If ($b –eq $false)
Created  without  $   Is  it  null?  
 
Set-Variable –name b –value 3.142 –option constant If ($c –eq $null)
 
Referenced  with  $  
$b  
 
 
 
  Creating  Objects  
  To  create  an  instance  of  a  com  object  
  New-­‐Object  -­‐comobject  <ProgID>  
  $a = New-Object –comobject "wscript.network"
  $a.username
 
  To  create  an  instance  of  a  .Net  Framework  object.    Parameters  can  be  passed  if  required  
  New-­‐Object  –type  <.Net  Object>  
  $d = New-Object -Type System.DateTime 2006,12,25
  $d.get_DayOfWeek()
 
Writing  t   o  Console   Capture  User  Input  
 
Variable  Name   Use  Read-­‐Host  to  get  user  input  
$a  
$a = Read-Host “Enter your name”
or     Write-Host "Hello" $a
Write-Host   $a –foregroundcolor “green”
   
  Miscellaneous  
  Line  Break  `  
Passing  
  C ommand   L ine   A rguments   Get-Process | Select-Object `
Passed     to  script  with  spaces   name, ID
myscript.ps1
  server1 benp Comments  #  
Accessed  in  script  by  $args  array   # code here not executed
$servername = $args[0] Merging  lines  ;  
$username = $args[1] $a=1;$b=3;$c=9
  Pipe  the  output  to  another  command  |  
Get-Service | Get-Member
 
Do  While  Loop   Do  Until  Loop  
  a  set  of  commands  while  a  condition  is  met  
Can  repeat   Can  repeat  a  set  of  commands  until  a  condition  is  met  
$a=1     $a=1  
 
Do  {$a;  $a++}   Do  {$a;  $a++}  
While  ($a    –lt  10)   Until  ($a  –gt  10)  
 
 
For  Loop     ForEach  -­‐  Loop  Through  Collection  of  Objects  
Repeat  the  
  same  steps  a  specific  number  of  times   Loop  through  a  collection  of  objects  
For ($a=1; 1   $a –le 10; $a++) Foreach ($i in Get-Childitem c:\windows)
{$a}   {$i.name; $i.creationtime}
   
 
 
   
If  Statement   Switch  Statement  
Run  a  specific  set  of  code  given  specific  conditions   Another  method  to  run  a  specific  set  of  code  given  
$a = "white" specific  conditions  
if ($a -eq "red") $a = "red"
{"The colour is red"} switch ($a)
elseif ($a -eq "white") {
{"The colour is white"} "red" {"The colour is red"}
else "white"{"The colour is white"}
{"Another colour"} default{"Another colour"}
}

Reading  From  a  File   Writing  to  a  Simple  File  


Use  Get-­‐Content  to  create  an  array  of  lines.    Then  loop   Use  Out-­‐File  or  >  for  a  simple  text  file  
through  array   $a = "Hello world"
$a = Get-Content "c:\servers.txt" $a | out-file test.txt
foreach ($i in $a) Or  use  >  to  output  script  results  to  file  
{$i} .\test.ps1 > test.txt

Writing  to  an  Html  File  


Use  ConvertTo-­‐Html  and  >  
$a = Get-Process
$a | Convertto-Html -property Name,Path,Company > test.htm

Writing  to  a  CSV  File  


Use  Export-­‐Csv  and  Select-­‐Object  to  filter  output  
$a = Get-Process
$a| Select-Object Name,Path,Company | Export-Csv -path test.csv
 

Load  a  Snap  In  


Load  a  Snap  in  for  added  functionality,  supressing  error  info  if  the  snap  in  is  already  loaded.  
Add-­‐PSSnapin  microsoft.sharepoint.powershell  -­‐ErrorAction  SilentlyContinue  

Working  With  Shortened  commands  (Aliases)  


Use  Get-­‐Alias  to  list  out  all  commands  with  shortened  alternatives  
Get-Alias
Find  the  long  form  of  a  command  from  its  alias:  
Get-Alias –name dir
Find  all  aliases  of  a  form  of  a  command  from  its  alias:  
Get-Alias –Definition “Get-ChildItem”

Refining  output  
Where-­‐Object  (Where)   Sort-­‐Object  (Sort)  
Where  is  used  to  limit  the  output  of  a  command   Limit  which  fields  are  returned  
Command  |  Where  {$_.ParameterName  –like  “value”}     Long  Form:  
$a = dir |Where {$_.PSIsContainer –eq $true} Dir | Sort-Object Name
Short  Form:  
Dir | Sort Name, Length

Select-­‐Object  (Select)  
Limit  which  fields  are  returned   Listing  Details  
Dir | Select Name, Length Sometimes  there  is  more  than  is  shown  by  default  
Limit  how  many  results  are  returned   Format-­‐List  outputs  more  fields,  in  a  list  format  
Dir | Select –First 3 Dir | Format-list
Dir | fl

Chaining  Multiple  Commands  


Multiple  commands  and  refiners  can  be  used  to  get  just  the  right  output:  
Dir | where {$_.PSIsContainer –eq $true} | Sort name | Select –first 3

 
Learning  about  a  result  by  using  where,  the  dot  (.)  and  tab  
Some  commands  return  complex  results  that  can  be  further  broken  down.  
It  is  often  helpful  to  narrow  down  the  results  to  just  one  item,  and  assign  that  one  result  to  a  variable  so  
that  you  can  inspect  its  properties.  
$d = Dir #returns too much
$d = Dir | select –first #better, returns one entry
At  this  point  you  can  type  $d.  and  hit  the  tab  key  repeatedly  to  see  the  different  properties.  
$d.(tab) #starts to list the properties such as $d.name, $d.fullname
Another  example,  using  where  to  pick  the  specific  result  to  inspect  
$d = Dir | Where {$_.name –eq “Windows”}

You might also like