MSDN Documentation

Microsoft Developer Network

PowerShell for Beginners: Cheat Sheet

Welcome to this essential cheat sheet designed to help you quickly get started with PowerShell. Whether you're managing Windows servers, automating tasks, or exploring the .NET framework, PowerShell is your key.

Getting Started: Basic Commands

These are the fundamental commands you'll use most often.

Getting Help

The first command you should always know is how to get help!

Get-Help Get-Command  # Get help on a specific cmdlet
Get-Help Get-Command -Examples # See examples
Get-Help Get-Command -Full # See all details
Get-Help *Verb-* # Find cmdlets with a specific verb
Update-Help # Download latest help files (run as Administrator)

Working with Objects and Pipelines

PowerShell is object-oriented. Commands output objects that can be piped to other commands.

Get-Process | Get-Member # See what properties and methods a process object has
Get-Service | Where-Object {$_.Status -eq 'Running'} # Filter running services
Get-Service | Sort-Object -Property Name # Sort services by name
Get-Process | Select-Object -Property Name, ID, CPU # Select specific properties of processes

Basic Cmdlets

Core cmdlets for managing your system.

Get-Command # List available commands (cmdlets, functions, aliases, etc.)
Get-Command -Noun Process # List commands related to the 'Process' noun
Get-Help Get-Process # Get help for the Get-Process cmdlet
Get-Process # List running processes
Get-Service # List services
Get-NetIPAddress # List IP addresses on your machine
Get-ChildItem (or dir, ls) # List files and folders (similar to 'dir' in cmd)
New-Item -Path .\MyFolder -ItemType Directory # Create a new directory
Remove-Item .\MyFile.txt # Remove a file or directory
Copy-Item .\source.txt .\destination.txt # Copy a file
Move-Item .\oldname.txt .\newname.txt # Move/Rename a file
Get-Date # Display the current date and time
Get-Location (or pwd) # Display the current directory
Set-Location C:\Users # Change the current directory

Variables and Data Types

Store and manipulate data easily.

Creating and Using Variables

$myVariable = "Hello, PowerShell!" # String variable
$number = 123 # Integer variable
$trueOrFalse = $true # Boolean variable
Write-Host $myVariable # Display variable content
$processName = "notepad"
$notepadProcess = Get-Process -Name $processName
Write-Host "The process '$($notepadProcess.Name)' has ID: $($notepadProcess.Id)"

Common Data Types

Understanding types helps in filtering and manipulation.

  • String: Text (e.g., "Hello")
  • Integer: Whole numbers (e.g., 10)
  • Double: Decimal numbers (e.g., 3.14)
  • Boolean: $true or $false
  • DateTime: Dates and times (e.g., (Get-Date))
  • Array: Ordered list of items (e.g., $myArray = 1, 2, 3)
  • HashTable: Key-value pairs (e.g., $myHash = @{Key1="Value1"; Key2="Value2"})

Control Flow and Logic

Make your scripts dynamic and intelligent.

Conditional Statements (If/ElseIf/Else)

$score = 85
if ($score -ge 90) {
    Write-Host "Grade: A"
} elseif ($score -ge 80) {
    Write-Host "Grade: B"
} else {
    Write-Host "Grade: C or lower"
}

Loops (ForEach)

Iterate over collections.

$users = "Alice", "Bob", "Charlie"
foreach ($user in $users) {
    Write-Host "Processing user: $user"
}

Get-Process | ForEach-Object {
    Write-Host "Name: $($_.Name), ID: $($_.Id)"
}

Loops (For)

Iterate a specific number of times.

for ($i = 0; $i -lt 5; $i++) {
    Write-Host "Iteration: $i"
}

Working with Files and Text

Essential for data processing and configuration.

Reading and Writing Files

# Read all lines from a file into an array
$lines = Get-Content -Path "C:\temp\myfile.txt"
# Read the entire file content as a single string
$content = Get-Content -Path "C:\temp\myfile.txt" -Raw

# Write to a file (overwrites existing content)
"This is the first line." | Set-Content -Path "C:\temp\newfile.txt"
# Append to a file
"This is another line." | Add-Content -Path "C:\temp\newfile.txt"

Filtering Text (Select-String)

Find lines containing specific text.

Get-Content -Path "C:\temp\logfile.txt" | Select-String -Pattern "Error"
Get-ChildItem -Path "C:\logs" -Filter "*.log" | Select-String -Pattern "Warning"

Common Aliases

Shortcuts for commonly used cmdlets.

Tips for Success