Uploaded by Tyler Modjeski

Comparitech-Powershell-cheatsheet

advertisement
PowerShell Cheat Sheet
Common cmdlets
Import, Export, Convert
Export-CliXML
ConvertTo-XML
Export-CSV
ConvertTo-CSV
Import-CliXML
ConvertTo-HTML
Import-CSV
ConvertFrom-CSV
cd, chdir, sl
cat, gc, type
ac
sc
Basic Commands
copy, cp, cpi
del,
erase,
rd,
ri, rm, rmdir
Cmdlet Commands built into shell written in .NET
Flow Control
mi, move, mv
Functions Commands written in PowerShell language
si
If(){} Elseif(){ } Else{ }
Parameter Argument to a Cmdlet/Function/Script
ni
while(){}
Alias Shortcut for a Cmdlet or Function
sleep
For($i=0; $i -lt 10; $i++){}
Scripts Text files with .ps1 extension
sajb
Foreach($file in dir C:\){$file.name}
Applications Existing windows programs
compare, diff
1..10 | foreach{$_}
Pipelines Pass objects Get-process word | Stop-Process
group
Ctrl+c Interrupt current command
curl,
iwr,
wget
Left/right Navigate editing cursor
Comments, Escape Characters
measure
Ctrl+left/right Navigate a word at a time
nal
Home / End End Move to start / end of line
#Comment Comment
rvpa
Up/down Move up and down through history
<#comment#> Multiline Comment
rujb
Insert Toggles between insert/overwrite mode
"`"test`"" Escape char `
set, sv
F7 Command history in a window
`t Tab
shcm
Tab / Shift-Tab Command line completion
`n New line
sort
` Line continue
sasv
Variables
saps, start
sujb
Parameters
$var = "string" Assign variable
wjb
$a,$b = 0 or $a,$b = 'a','b' Assign multiple variables
-Confirm Prompt whether to take action
?, where
$a,$b = $b,$a Flip variables
-WhatIf Displays what command would do
echo, write
$var=[int]5 Strongly typed variable
gcm
foreach,%
sort
where, ?
diff, compare
dir, ls, gci
gi
copy, cp, cpi
move, mv, mi
del, rm
rni, ren
fFt
fl
gcim
cat, gc, type
sc
h, history, ghy
ihy, r
gp
sp
pwd, gl
gm
sls
cd, chdir, sl
cls, clear
Aliases
Get-Command
Foreach-Object
Sort-Object
Where-Object
Compare-Object
Get-ChildItem
Get-Item
Copy-Item
Move-Item
Remove-Item
Rename-Item
Format-Table
Format-List
Get-CimInstance
Get-Content
Set-Content
Get-History
Invoke-History
Get-ItemProperty
Set-ItemProperty
Get-Location
Get-Member
Select-String
Set-Location
Clear-Host
Arrays Objects
$arr = "a", "b" Array of strings
$arr = @() Empty array
Assign values to variable
$arr[5] Sixth array element
Connect expressions / statements
$arr[-3..-1] Last three array elements
Equal, not equal
$arr[1,4+6..9] Elements at index 1,4, 6-9
Greater than, greater than or equal
$arr[1] += 200 Add to array item value
Less than, less than or equal
$z = $arA + $arB Two arrays into single array
“Hi” -replace “H”, “P”
[pscustomobject]@{x=1;z=2} Create custom object
Regular expression match
(Get-Date).Date Date property of object
Wildcard matching
Check if value in array
Writing output and reading
Reverse of contains, notcontains.
Assignment, Logical, Comparison
Help
Get-Command
Get-Command -Module RGHS
Get-Command Get-p*
Get-help get-process
Get-Process | Get-Member
Get-Process| format-list -properties *
Set-Location
Get-Content
Add-Content
Set-Content
Copy-Item
Remove-Item
Move-Item
Set-Item
New-Item
Start-Sleep
Start-Job
Compare-Object
Group-Object
Invoke-WebRequest
Measure-Object
New-Alias
Resolve-Path
Resume-Job
Set-Variable
Show-Command
Sort-Object
Start-Service
Start-Process
Suspend-Job
Wait-Job
Where-Object
Write-Output
CommonCommon
Aliases
=, +=, -=, ++,--and, -or, -not,!
-eq, -ne
-gt, -ge
-lt, -le
-replace
-match, -notmatch
-like, -notlike
-contains, -notcontains
-in, -notin
Get all commands
Get all commands in RGHS module
Get all commands starting with get-p
Get help for command
Get members of the object
Get-Process as list with all properties
Scripts
Set-ExecutionPolicy -ExecutionPolicy
."\\c-is-ts-91\c$\scripts\script.ps1”
&"\\c-is-ts-91\c$\scripts\script.ps1"
.\Script.ps1
$profile
Bypass Set execution policy to allow all scripts
Run Script.PS1 script in current scope
Run Script.PS1 script in script scope
Run Script.ps1 script in script scope
Your personal profile that runs at launch
"This displays a string"
Write-Host "color" -ForegroundColor Red -NoNewLine
$age = Read-host "Please enter your age"
$pwd = Read-host "Please enter your password" -asSecureString
Clear-Host
String is written directly to output
String with colors, no new line at end
Set $age variable to input from user
Read in $pwd as secure string
Clear console
Download