Powershell Basic Cheat Sheet

ADVERTISEMENT

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
$a,$b = $b,$a
Flip variables
Export-CSV
Import-CSV
Sort
Sort-Object
language. To run it, click Start, type PowerShell, run PowerShell ISE
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
Diff,compare
Compare-Object
start with a dash.
Assignment, Logical, Comparison Operators
Flow Control
Dir, ls, gci
Get-ChildItem
Basics
=,+=,-=,++,--
Assign values to variable
If(){} Elseif(){ } Else{ }
Gi
Get-Item
Cmdlet
Commands built into shell written in .NET
-and,-or,-not,!
Connect expressions / statements
while(){}
Copy,cp,cpi
Copy-Item
Functions
Commands written in PowerShell language
-eq, -ne
Equal, not equal
For($i=0; $i -lt 10; $i++){}
Move,mv,mi
Move-Item
Parameter
Argument to a Cmdlet/Function/Script
-gt, -ge
Greater than, greater than or equal
Foreach($file in dir C:\){$file.name}
Del,rm
Remove-Item
Alias
Shortcut for a Cmdlet or Function
-lt, -le
Less than, less than or equal
1..10 | foreach{$_}
Rni,ren
Rename-Item
Scripts
Text files with .ps1 extension
-replace
“Hi” -replace “H”, “P”
Ft
Format-Table
Applications
Existing windows programs
Comments, Escape Characters
-match,-notmatch
Regular expression match
Fl
Format-List
Pipelines |
Pass objects Get-process word | Stop-Process
#Comment
Comment
-like,-notlike
Wildcard matching
Gcim
Get-CimInstance
Ctrl+c
Interrupt current command
<#comment#> Multiline Comment
-contains,-notcontains Check if value in array
Cat,gc,type
Get-Content
Left/right
Navigate editing cursor
"`"test`""
Escape char `
-in, -notin
Reverse of contains,notcontains.
Sc
Set-Content
Ctrl+left/right
Navigate a word at a time
`t
Tab
h,history,ghy
Get-History
Home / End
Move to start / end of line
Parameters
`n
New line
Ihy,r
Invoke-History
Up/down
Move up and down through history
-Confirm
Prompt whether to take action
`
Line continue
Gp
Get-ItemProperty
Insert
Toggles between insert/overwrite mode
-WhatIf
Displays what command would do
Sp
Set-ItemProperty
F7
Command history in a window
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

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go