Skip to content

PowerShell: Common Commands Reference

Common PowerShell commands and patterns for system administration and automation on Windows.

Query Active Directory for user details:

Terminal window
Get-ADUser -Server localhost -Identity cs -Properties *

Get specific properties:

Terminal window
Get-ADUser -Identity cs -Properties DisplayName, EmailAddress, Department

Search for users by name:

Terminal window
Get-ADUser -Filter "Name -like '*John*'" -Properties DisplayName, EmailAddress

List user’s group memberships:

Terminal window
Get-ADPrincipalGroupMembership -Identity cs | Select-Object Name

Add user to group:

Terminal window
Add-ADGroupMember -Identity "Developers" -Members cs

Remove user from group:

Terminal window
Remove-ADGroupMember -Identity "Developers" -Members cs -Confirm:$false
Terminal window
# Change directory
Set-Location C:\Users
# List files
Get-ChildItem
# List with details
Get-ChildItem | Format-Table Name, Length, LastWriteTime
# Recursive search
Get-ChildItem -Recurse -Filter "*.log"
Terminal window
# Copy files
Copy-Item source.txt destination.txt
# Move files
Move-Item oldname.txt newname.txt
# Delete files
Remove-Item file.txt
# Create directory
New-Item -ItemType Directory -Path C:\NewFolder
Terminal window
# Read file content
Get-Content file.txt
# Write to file
"Hello World" | Out-File file.txt
# Append to file
"New line" | Add-Content file.txt
# Read as single string
Get-Content file.txt -Raw
Terminal window
# List running processes
Get-Process
# Filter by name
Get-Process -Name notepad
# Stop process
Stop-Process -Name notepad
# Start process
Start-Process notepad.exe
# Get process by ID
Get-Process -Id 1234
Terminal window
# List all services
Get-Service
# Get specific service
Get-Service -Name wuauserv
# Start service
Start-Service -Name wuauserv
# Stop service
Stop-Service -Name wuauserv
# Restart service
Restart-Service -Name wuauserv
# Set service startup type
Set-Service -Name wuauserv -StartupType Automatic
Terminal window
# Get IP configuration
Get-NetIPConfiguration
# Test connection
Test-Connection google.com
# Get network adapters
Get-NetAdapter
# DNS lookup
Resolve-DnsName google.com
Terminal window
# Download file
Invoke-WebRequest -Uri "https://example.com/file.zip" -OutFile "file.zip"
# Download and execute script
Invoke-Expression (Invoke-WebRequest -Uri "https://example.com/script.ps1").Content
Terminal window
# String length
$str = "Hello World"
$str.Length
# Substring
$str.Substring(0, 5) # "Hello"
# Replace
$str.Replace("World", "PowerShell")
# Split
$str.Split(" ")
# Join
$words = @("Hello", "World")
$words -join " "
# Case conversion
$str.ToUpper()
$str.ToLower()
Terminal window
# Create array
$arr = @(1, 2, 3, 4, 5)
# Access elements
$arr[0]
# Add elements
$arr += 6
# Filter array
$arr | Where-Object { $_ -gt 3 }
# Transform array
$arr | ForEach-Object { $_ * 2 }
# Sort array
$arr | Sort-Object -Descending
Terminal window
# Create hash table
$hash = @{
Name = "John"
Age = 30
City = "New York"
}
# Access values
$hash["Name"]
$hash.Age
# Add key-value pair
$hash["Email"] = "john@example.com"
# Remove key
$hash.Remove("City")
Terminal window
# Basic pipeline
Get-Process | Where-Object { $_.CPU -gt 100 } | Sort-Object CPU -Descending
# Select specific properties
Get-Service | Select-Object Name, Status
# Group by property
Get-Process | Group-Object ProcessName
# Measure objects
Get-ChildItem | Measure-Object -Property Length -Sum
Terminal window
try {
Get-Item "C:\nonexistent.txt" -ErrorAction Stop
}
catch {
Write-Host "Error: $($_.Exception.Message)"
}
finally {
Write-Host "Cleanup operations"
}
Terminal window
# Execute command on remote machine
Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Process }
# Start interactive session
Enter-PSSession -ComputerName Server01
# Execute script on remote machine
Invoke-Command -ComputerName Server01 -FilePath C:\Scripts\script.ps1
# Copy to remote session
Copy-Item -Path C:\local\file.txt -Destination C:\remote\ -ToSession $session
Terminal window
# Get environment variable
$env:PATH
# Set environment variable (current session)
$env:MY_VAR = "value"
# Set permanently (user)
[Environment]::SetEnvironmentVariable("MY_VAR", "value", "User")
# Set permanently (machine)
[Environment]::SetEnvironmentVariable("MY_VAR", "value", "Machine")
Terminal window
# Read registry value
Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion" -Name ProgramFilesDir
# Set registry value
Set-ItemProperty -Path "HKCU:\Software\MyApp" -Name "Setting" -Value "Value"
# Create registry key
New-Item -Path "HKCU:\Software\MyApp"
# Delete registry key
Remove-Item -Path "HKCU:\Software\MyApp"
Terminal window
# List scheduled tasks
Get-ScheduledTask
# Create scheduled task
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\script.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At 9am
Register-ScheduledTask -TaskName "MyTask" -Action $action -Trigger $trigger
# Run task
Start-ScheduledTask -TaskName "MyTask"
# Disable task
Disable-ScheduledTask -TaskName "MyTask"
Terminal window
# List all aliases
Get-Alias
# Common aliases
# ls -> Get-ChildItem
# cd -> Set-Location
# cat -> Get-Content
# rm -> Remove-Item
# cp -> Copy-Item
# mv -> Move-Item
Terminal window
# Get help for command
Get-Help Get-Process
# Get examples
Get-Help Get-Process -Examples
# Get detailed help
Get-Help Get-Process -Detailed
# Update help files
Update-Help
Terminal window
# Check current execution policy
Get-ExecutionPolicy
# Set execution policy (run as administrator)
Set-ExecutionPolicy RemoteSigned
# Bypass for single script
PowerShell.exe -ExecutionPolicy Bypass -File script.ps1