FINDING CMDLETS AND HELP USEFUL CMDLETS LOOPS & BRANCHES
Get-Command List available commands. Use -Module, -Noun, How To… Commands Loop/Branch How To Use -Verb. Wildcards help too Zip Files Compress-Archive, Expand-Archive If/Then/Else If ($true) { <this> } else { <that> } Get-Member List properties and methods of an object Date/Time Get-Date, Set-Date, Get-TimeZone, Set-TimeZone For For ($a=0; $a -lt 10; $a++) { <this> } Get-Help Help for a command. Use -Online to get latest Event Logs Get-WinEvent, New-WinEvent Do…While Do { <this> } While ($evaluation) Performance Get-Counter, Export-Counter, Import-Counter Do…Until Do { <this> } Until ($evaluation) Clipboard Get-Clipboard, Set-Clipboard While While ($evaluation) { <this> } WORKING WITH OBJECTS Reboot Restart-Computer Foreach Foreach ($a in $b) { <this $a> } Send Output Out-Printer, Out-Null, Out-File Switch Switch ($a) { Common pattern: Get | Filter/Group/Sort | Modify/Delete/Output/Convert User Input Read-Host “one” { <this happens if $a is “one”> } Where-Object Filters objects based on value of property Use Jobs Start-Job, Stop-Job, Get-Job, Receive-Job, Remove-Job Default { <this $a is none of above>} Select-Object Choose properties of an object to include in Wait Start-Sleep } pipeline Map Drives Get-PSDrive, New-PSDrive, Remove-PSDrive Group-Object Group based on property values Navigate Get-Location, Set-Location, Test-Path Sort-Object Sort results by property values File/Folders New-Item, Get-Item, Get-ChildItem, ARRAYS Foreach-Object Act on each object in pipeline Get-Content, Set-Content -Parallel Act on each object in pipeline at the same time Move-Item, Rename-Item, Copy-Item, Remove-Item How To… Commands Measure-Object Measure property values or number of objects Display in Out-Gridview (with -OutputMode and -Passthru) to Create Array $a = @() GUI form select one or more items and return to shell Single Item Array $a = @(“one”) Item Reference $a[index] (0 is first) BUILT-IN VARIABLES Range Reference $a[0..4] (Returns first 5) COMMON PARAMETERS Last Item Reference $a[-1] $Args Arguments passed into script. $error Array of errors. $Error[0] is latest. -WHATIF Don’t make the changes, but output what would $host Details on application running PS -CONFIRM Prompt before making changes -VERBOSE Display verbose output MODULES AND PACKAGES $IsLinux Returns TRUE on Linux OS $isMacos Returns TRUE on Mac OS -DEBUG Display debug-level output -ERRORACTION Override $ErrorActionPreference variable Find-Module Search PSGallery for PowerShell modules $IsWindows Returns TRUE on Windows OS Find-Package Search PSGallery, nuget.org for software $Profile Path to PowerShell profiles -OUTVARIABLE Redirect output to a variable -? Display help for the cmdlet Get-Module Find modules/packages installed on system $PSBoundParameterValues List parameters and current values. Get-Package Software installed by package management $PSCommandPath Full path of script being run Other Verbs Install, Uninstall, Update $PSItem / $_ Current object in the pipeline Register-PackageSource Allow package sources for installation $PSScriptRoot Directory the script is run from KEYBOARD SHORTCUTS Install-PackageProvider Allow additional package providers (Gist) or $PSVersionTable Details on PowerShell version specific versions Esc Clear line Tab Complete partially entered cmdlet/parameter CTRL+C Stop processing current command OPERATORS Up/Down Arrow Navigate command history ADVANCED FUNCTION CTRL+S/CTRL+R Search forward/reverse through history Pipeline |, ?? (If error), && (If success) PARAMETER ATTRIBUTES CTRL+ALT+? Show all keybindings Arithmetic +, -, *, /, % Mandatory Will prompt if missing Assignment =, +=, -=, *=, /=, %= Position Allows params in order instead Null Coalescing ?? STRINGS of by name Comparison -eq, -ne, -gt, -lt, -le, -ge ValueFromPipeline Allows pipeline input to How To… Commands/Examples parameter Wildcard Compare -like, -notlike Grep / Search Text Select-String ValueFromPipelineByPropertyName Pipeline accepted if property Regex Compare -match, -notmatch, -replace Split into array “one,two,three” -split “,” name matches Join “one”, “two”, “three” -join “, and a “ HelpMessage Sets param help msg Contain Comparison -in, -notin, -contains, -notcontains Replace “http://mysite.com” -replace “http:”,”https:” ValidateSet(“Choice1”,”Choice2”) Gives choice, allows tab- Logical -and, -or, -xor, -not, ! complete # Decimal Places “Pi is {0:N2}” -f [Math]::Pi Type -is, -isnot, -as Format Currency “The price is {0:C}” -f 1.23 ValidateScript({ evaluation script }) Processes an evaluation script Ternary (statement) ? (if true) : (if false) Format All Caps $a = “I’m yelling”; $a.ToUpper() ValidateRange([1..10]) Enforces param values in range Format All Lower $a = “TOO LOUD”; $a.ToLower() Insert Characters $a = “abcghij”; $a.Insert(3,"def")