PowerShell Cheat Sheet v1.0 Importing, Exporting, & Converting

[Pages:1]PowerShell Cheat Sheet v1.0

By: Fernando Tomlinson @Wired_Pulse

Common Aliases

%, ForEach

ForEach-Object

?, Where

Where-Object

Cat, gc, type Cd Clear, cls Compare, diff Copy, cp Curl, iwr, wget Dir Echo Erase, rm, del Fl Ft Gcm Gm Gwmi H, history Icm Iex Kill Move, mv Ogv Pwd Ren, rni Select Sort

Get-Content Set-Location Clear-Host Compare-Object Copy-Item Invoke-Webrequest Get-ChildItem Write-Output Remove-Item Format-List Format-Table Get-Command Get-Member Get-WMIObject Get-History Invoke-Command Invoke-Expression Stop-Process Move-Item Out-GridView Get-Location Rename-Item Select-Object Sort-Object

Cmdlets

Functions Parameter Alias Scripts Pipelines

Ctrl + C Ctrl + left / right Home /End Up /down

Basics Commands built into the shell written in .NET Commands written in PowerShell Arguments to a cmdlet/function/script Shortcut for a cmdlet or function Text files with .ps1 extension Sends objects from one command to the next Interrupt current command Navigate one word at a time

Move to start / end of a line Move up and down through the history

Assignment, Logical, & Comparison Operators

=, +=, -=, ++, --

Assign to a variable

+=, -= ++, --and, -or, -not, !

-eq, -ne -gt, -ge

-lt, -le

-replace -match, -notmatch -like, -notlike -contains, -notcontains -in, -notin

Appends to variable

Variable iteration

Connect expressions / statements

Equal, Not Equal

Greater than, greater than or equal

Less than, less than or equal

Replace one for one

Regular expression match

Wildcard matching

Check if value in array

Reverse of contains, notcontains

-Confirm -Whatif

Parameters Prompt user to take action Displays what the command would do

$var = "string"

Variables Assigns variable (string)

[int]$var = 7 $a = $b = $c = 11

Assigns variable as int

Assigns each variable with 11

Arrays and Objects

$array = 1, 2, 3, 4 Array of integers

$array = "a","b","c" Array of strings

$array = @()

Empty array

$array[3]

Fourth array element

$array[-5..-1]

Last three array elements

(Get-Process).name Name property of object

Help

Get-Help

Gets helps for cmdlet

Help -examples

Gets examples for cmdlet

Get-Command ?module ABC

Lists all commands in the ABC module

Get-Command

Get a list of all commands

Get-Command Get-*

Lists all commands starting with "get-"

Get-Service | Get-Member Get-members of the object

Get-Service | Format-List * List of all properties

Scripts

Set-ExecutionPolicy ? ExecutionPolicy Unrestricted

Allows scripts to run

.\script.ps1

Runs script

C:\script.ps1

Runs script

$profile

Your personal profile that runs at launch

"Hello, I am a string"

Writing output and reading data String written directly to output

Write-Host "some text" ?foregroundcolor Blue $day = Read-Host "Enter day" Get-Process | Out-GridView

Displays text as blue Sets $day variable to input from user Displays processes in an Excel-like output

Importing, Exporting, & Converting Convert-ToCliXML Convert-ToHTML

Convert-ToCSV

Convert-FromCSV

Export-CSV

Import-CSV

Export-CliXML

Import-CliXML

Comments & Escape Characters

# Comment

Comment

Multiline comment

"`"Escape`"" `n `r `t `v `

Escapes ` character New line Carriage return Horizontal tab Vertical tab Line continue

Flow Control If(){} ElseIf(){} Else{}

While(){}

Do{} while() ForEach($thing in $things){} For($i=0; $i ?gt 0; $i--){} 1..10 | ForEach{$_}

Set-Location

Cmdlets Add-Content

Get-Content Out-File Copy-Item Move-Item New-Item Get-Eventlog Start-Sleep Test-Connection Get-ADUser Get-WMIObject

Set-Content Out-String Remove-Item Set-Item Get-Date Get-WinEvent Compare-Object Test-Path Get-SMBShare

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download