PowerShell Common Commands

powershell windows scripting reference


Getting Help

# Get help for command
Get-Help Get-Process
Get-Help Get-Process -Examples
Get-Help Get-Process -Full
 
# Find commands
Get-Command *process*
Get-Command -Module ActiveDirectory
 
# Get command aliases
Get-Alias ls
Get-Alias -Definition Get-ChildItem

Directory Operations

# Current directory
Get-Location           # pwd
Set-Location C:\Users  # cd
 
# List files
Get-ChildItem          # ls, dir
Get-ChildItem -Recurse
Get-ChildItem -Force   # Include hidden
Get-ChildItem *.log
 
# Create directory
New-Item -ItemType Directory -Path "NewFolder"
mkdir NewFolder        # Alias
 
# Create file
New-Item -ItemType File -Path "file.txt"
"content" | Out-File file.txt

File Operations

# Copy
Copy-Item source.txt destination.txt
Copy-Item -Recurse ./folder ./backup
 
# Move
Move-Item source.txt ./archive/
 
# Delete
Remove-Item file.txt
Remove-Item -Recurse ./folder
Remove-Item -Force file.txt  # Skip confirmation
 
# Read file
Get-Content file.txt
Get-Content file.txt -Tail 10     # Last 10 lines
Get-Content file.txt -Wait        # Follow (like tail -f)
 
# Write file
"text" | Out-File file.txt
"text" | Add-Content file.txt     # Append
Set-Content file.txt "text"       # Overwrite
# Find files by name
Get-ChildItem -Recurse -Filter "*.log"
 
# Find files containing text
Get-ChildItem -Recurse | Select-String "error"
 
# Find large files
Get-ChildItem -Recurse | Where-Object { $_.Length -gt 100MB }

Process Management

# List processes
Get-Process
Get-Process chrome
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10
 
# Start process
Start-Process notepad
Start-Process -FilePath "app.exe" -ArgumentList "-arg1 value"
 
# Stop process
Stop-Process -Name notepad
Stop-Process -Id 1234
Get-Process notepad | Stop-Process

Service Management

# List services
Get-Service
Get-Service | Where-Object Status -eq Running
Get-Service -Name "wuauserv"
 
# Start/Stop/Restart
Start-Service -Name "ServiceName"
Stop-Service -Name "ServiceName"
Restart-Service -Name "ServiceName"
 
# Change startup type
Set-Service -Name "ServiceName" -StartupType Automatic

Network Commands

Basic Network

# IP configuration
Get-NetIPConfiguration
Get-NetIPAddress
 
# DNS lookup
Resolve-DnsName google.com
Resolve-DnsName -Type MX google.com
 
# Test connectivity
Test-Connection google.com        # ping
Test-NetConnection google.com -Port 443
Test-NetConnection -ComputerName server -TraceRoute
 
# Network adapters
Get-NetAdapter
Get-NetAdapter | Where-Object Status -eq Up

Firewall

# List firewall rules
Get-NetFirewallRule | Where-Object Enabled -eq True
 
# Add firewall rule
New-NetFirewallRule -DisplayName "Allow Port 8080" -Direction Inbound -Protocol TCP -LocalPort 8080 -Action Allow
 
# Remove rule
Remove-NetFirewallRule -DisplayName "Allow Port 8080"

HTTP Requests

# GET request
Invoke-WebRequest https://api.example.com
(Invoke-WebRequest https://api.example.com).Content
 
# POST with JSON
$body = @{name="John"; email="john@example.com"} | ConvertTo-Json
Invoke-RestMethod -Uri "https://api.example.com/users" -Method Post -Body $body -ContentType "application/json"
 
# Download file
Invoke-WebRequest -Uri "https://example.com/file.zip" -OutFile "file.zip"

Text Processing

String Operations

# Select-String (grep equivalent)
Get-Content log.txt | Select-String "error"
Select-String -Path "*.log" -Pattern "error"
Select-String -Path "*.log" -Pattern "error" -CaseSensitive
 
# Replace
(Get-Content file.txt) -replace "old", "new" | Set-Content file.txt
 
# Split/Join
"a,b,c" -split ","
"a","b","c" -join ","

Filtering and Selecting

# Where-Object (filter)
Get-Process | Where-Object CPU -gt 100
Get-Process | Where-Object { $_.Name -like "*chrome*" }
 
# Select-Object (choose properties)
Get-Process | Select-Object Name, CPU, Memory
Get-Process | Select-Object -First 5
Get-Process | Select-Object -Property *  # All properties
 
# Sort-Object
Get-Process | Sort-Object CPU -Descending
Get-Process | Sort-Object Name, CPU
 
# Group-Object
Get-Process | Group-Object Name
 
# Measure-Object
Get-Process | Measure-Object CPU -Sum -Average

JSON/XML Processing

# Parse JSON
$json = Get-Content data.json | ConvertFrom-Json
$json.property
 
# Create JSON
@{name="John"; age=30} | ConvertTo-Json
 
# Parse XML
[xml]$xml = Get-Content data.xml
$xml.root.element

Variables and Objects

# Variables
$name = "John"
$number = 42
$array = @(1, 2, 3)
$hash = @{key1="value1"; key2="value2"}
 
# Environment variables
$env:PATH
$env:USERNAME
$env:COMPUTERNAME
[Environment]::SetEnvironmentVariable("VAR", "value", "User")
 
# Object properties
$obj | Get-Member                  # List all members
$obj.PropertyName                  # Access property
$obj | Select-Object -ExpandProperty PropertyName

Active Directory

# Import module
Import-Module ActiveDirectory
 
# Find users
Get-ADUser -Identity jsmith
Get-ADUser -Filter {Name -like "*john*"}
Get-ADUser -Filter * -Properties *
 
# Find computers
Get-ADComputer -Filter *
Get-ADComputer -Identity "WORKSTATION01"
 
# Find groups
Get-ADGroup -Filter *
Get-ADGroupMember -Identity "Domain Admins"
 
# User group membership
Get-ADPrincipalGroupMembership -Identity jsmith

Remote Management

# Enter remote session
Enter-PSSession -ComputerName server01
 
# Run command remotely
Invoke-Command -ComputerName server01 -ScriptBlock { Get-Process }
 
# Run on multiple servers
Invoke-Command -ComputerName server01,server02 -ScriptBlock { Get-Service }
 
# Copy to/from remote
Copy-Item -Path "file.txt" -Destination "\\server01\c$\temp\"

Scheduled Tasks

# List scheduled tasks
Get-ScheduledTask
Get-ScheduledTask -TaskName "TaskName"
 
# Create task
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File C:\script.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At 9am
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "DailyScript"
 
# Run task
Start-ScheduledTask -TaskName "TaskName"

Output Formatting

# Table (default)
Get-Process | Format-Table
Get-Process | Format-Table -AutoSize
Get-Process | Format-Table Name, CPU, Memory
 
# List
Get-Process | Format-List
Get-Process | Format-List *
 
# Export to CSV
Get-Process | Export-Csv processes.csv -NoTypeInformation
 
# Export to JSON
Get-Process | ConvertTo-Json | Out-File processes.json
 
# Grid view (GUI)
Get-Process | Out-GridView

Script Execution Policy

# Check policy
Get-ExecutionPolicy
 
# Set policy (requires admin)
Set-ExecutionPolicy RemoteSigned
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
 
# Bypass for single script
powershell -ExecutionPolicy Bypass -File script.ps1

Aliases Reference

AliasCommand
ls, dirGet-ChildItem
cd, chdirSet-Location
pwdGet-Location
cp, copyCopy-Item
mv, moveMove-Item
rm, delRemove-Item
cat, typeGet-Content
cls, clearClear-Host
psGet-Process
killStop-Process
curl, wgetInvoke-WebRequest
whereWhere-Object
selectSelect-Object
sortSort-Object
%ForEach-Object
?Where-Object