You are on page 1of 1

Variables Importing, Exporting, Converting Aliases for common commands

PowerShell Basic Cheat Sheet $var = "string" Assign variable Export-CliXML Import-CliXML Gcm Get-Command
$a,$b = 0 or $a,$b = 'a','b' Assign multiple variables ConvertTo-XML ConvertTo-HTML Foreach,% Foreach-Object
PowerShell is a task based command line shell and scripting
language. To run it, click Start, type PowerShell, run PowerShell ISE $a,$b = $b,$a Flip variables Export-CSV Import-CSV Sort Sort-Object
or PowerShell as Administrator. $var=[int]5 Strongly typed variable ConvertTo-CSV ConvertFrom-CSV Where,? Where-Object
Commands are written in verb-noun form, and named parameters
start with a dash. Diff,compare Compare-Object
Assignment, Logical, Comparison Operators Flow Control
Basics Dir, ls, gci Get-ChildItem
=,+=,-=,++,-- Assign values to variable If(){} Elseif(){ } Else{ }
Cmdlet Commands built into shell written in .NET Gi Get-Item
-and,-or,-not,! Connect expressions / statements while(){}
Functions Commands written in PowerShell language Copy,cp,cpi Copy-Item
-eq, -ne Equal, not equal For($i=0; $i -lt 10; $i++){}
Parameter Argument to a Cmdlet/Function/Script Move,mv,mi Move-Item
-gt, -ge Greater than, greater than or equal Foreach($file in dir C:\){$file.name}
Alias Shortcut for a Cmdlet or Function Del,rm Remove-Item
-lt, -le Less than, less than or equal 1..10 | foreach{$_}
Scripts Text files with .ps1 extension Rni,ren Rename-Item
-replace Hi -replace H, P
Applications Existing windows programs Comments, Escape Characters Ft Format-Table
-match,-notmatch Regular expression match
Pipelines | Pass objects Get-process word | Stop-Process #Comment Comment Fl Format-List
-like,-notlike Wildcard matching
Ctrl+c Interrupt current command <#comment#> Multiline Comment Gcim Get-CimInstance
-contains,-notcontains Check if value in array
Left/right Navigate editing cursor "`"test`"" Escape char ` Cat,gc,type Get-Content
-in, -notin Reverse of contains,notcontains.
Ctrl+left/right Navigate a word at a time `t Tab Sc Set-Content

Home / End Move to start / end of line Parameters `n New line h,history,ghy Get-History

Up/down Move up and down through history -Confirm Prompt whether to take action ` Line continue Ihy,r Invoke-History

Insert Toggles between insert/overwrite mode -WhatIf Displays what command would do Gp Get-ItemProperty

F7 Command history in a window Sp Set-ItemProperty


Cmdlets Arrays, Objects Pwd,gl Get-Location
Tab / Shift-Tab Command line completion
Get-EventLog Get-WinEvent $arr = "a", "b" Array of strings Gm Get-Member
Help Get-Date $arr = @() Empty array Sls Select-String
Get-Command Get all commands Start-Sleep Compare-Object $arr[5] Sixth array element Cd,chdir,sl Set-Location
Get-Command -Module RGHS Get all commands in RGHS module Start-Job Get-Credential $arr[-3..-1] Last three array elements Cls,clear Clear-Host
Get-Command Get-p* Get all commands starting with get-p Test-Connection New-PSSession $arr[1,4+6..9] Elements at index 1,4, 6-9
Cmdlets
Get-help get-process Get help for command Test-Path Split-Path $arr[1] += 200 Add to array item value
Set-Location
Get-Process | Get-Member Get members of the object Get-ADUser Get-ADComputer $z = $arA + $arB Two arrays into single array
Get-Content
Get-Process| format-list -properties * Get-Process as list with all properties Get-History New-ISESnippet [pscustomobject]@{x=1;z=2} Create custom object
Add-Content
Get-WMIObject Get-CimInstance (Get-Date).Date Date property of object
Set-Content
Writing output and reading input Scripts Out-File
"This displays a string" String is written directly to output Set-ExecutionPolicy -ExecutionPolicy Bypass Set execution policy to allow all scripts Out-String
Write-Host "color" -ForegroundColor Red -NoNewLine String with colors, no new line at end ."\\c-is-ts-91\c$\scripts\script.ps1" Run Script.PS1 script in current scope Copy-Item
$age = Read-host "Please enter your age" Set $age variable to input from user &"\\c-is-ts-91\c$\scripts\script.ps1" Run Script.PS1 script in script scope Remove-Item
$pwd = Read-host "Please enter your password" -asSecureString Read in $pwd as secure string .\Script.ps1 Run Script.ps1 script in script scope Move-Item
Clear-Host Clear console $profile Your personal profile that runs at launch Set-Item
Example command: dir C:\users\example -recurse -File | ?{$_.LastWriteTime -gt [datetime]::Today} | Select LastWriteTime,CreationTime,Length,FullName | sort LastWriteTime -descending | ft -AutoSize New-Item
This gets all files under C:\users\example, filters by lastwritetime today, only returns lastwritetime, creationtime, length and fullname, sorts by lastwritetime and outputs results in an autosized table