PowerShell Automation Lab

Master Windows automation and system administration with PowerShell

6 Exercises 90-120 minutes Beginner to Intermediate

Learning Objectives

1

PowerShell Fundamentals

Beginner

PowerShell is an object-oriented shell—commands return objects with properties and methods, not just text.

Concept Overview

# Get help on any cmdlet Get-Help Get-Process -Detailed Get-Help Get-Service -Examples # Discover commands Get-Command -Verb Get # All "Get" cmdlets Get-Command -Noun *Process* # Commands about processes # Explore object properties Get-Process | Get-Member # List all properties/methods Get-Service | Get-Member -MemberType Property # Variables $name = "PowerShell" $version = $PSVersionTable.PSVersion Write-Host "Running $name version $version"
Object Pipeline vs Text Pipeline

Unlike Bash where commands output text, PowerShell outputs objects. When you pipe to another cmdlet, you're passing rich objects with properties—not parsing text!

Tasks

  • Open PowerShell and check your version with $PSVersionTable
  • Use Get-Command to find all cmdlets with "Event" in the noun
  • Run Get-Help on 3 different cmdlets
  • Create variables for your name, favorite number, and a boolean
  • Use Get-Member to explore the properties of a DateTime object
# Exploring DateTime object $now = Get-Date $now | Get-Member $now.DayOfWeek $now.AddDays(30)

Related Visualizers

PowerShell Fundamentals
2

Navigation & File Operations

Beginner

PowerShell uses familiar navigation concepts but with object-aware cmdlets.

Concept Overview

# Navigation (aliases work too!) Get-Location # pwd Set-Location C:\Users # cd Get-ChildItem # ls / dir Get-ChildItem -Recurse -Filter *.txt # File operations New-Item -ItemType Directory -Name "TestFolder" New-Item -ItemType File -Name "test.txt" Copy-Item test.txt backup.txt Move-Item backup.txt .\Archive\ Remove-Item test.txt # Reading and writing files Get-Content file.txt # Read file Set-Content file.txt "New content" # Overwrite Add-Content file.txt "Appended" # Append
Bash/CMD PowerShell Cmdlet PowerShell Alias
ls / dir Get-ChildItem ls, dir, gci
cd Set-Location cd, sl
cat / type Get-Content cat, gc
cp / copy Copy-Item cp, copy
rm / del Remove-Item rm, del

Tasks

  • Navigate to your Documents folder and list all .txt files
  • Create a "PowerShellLab" folder with subfolders "Scripts" and "Logs"
  • Create a file, add content to it, then read it back
  • Find all files larger than 1MB in a directory recursively
  • Copy multiple files matching a pattern to a backup folder
Finding Large Files

Get-ChildItem -Recurse | Where-Object { $_.Length -gt 1MB }

3

The Pipeline: Filtering & Sorting

Intermediate

PowerShell's pipeline passes objects between cmdlets, enabling powerful data manipulation.

Concept Overview

# Where-Object: Filter objects Get-Process | Where-Object { $_.CPU -gt 100 } Get-Service | Where-Object Status -eq 'Running' # Select-Object: Choose properties Get-Process | Select-Object Name, CPU, WorkingSet -First 5 Get-Service | Select-Object -Property Name, Status # Sort-Object: Order results Get-Process | Sort-Object CPU -Descending | Select-Object -First 10 # Group-Object: Categorize Get-Service | Group-Object Status # Measure-Object: Statistics Get-ChildItem | Measure-Object -Property Length -Sum -Average # Combining operations Get-Process | Where-Object { $_.WorkingSet -gt 100MB } | Sort-Object WorkingSet -Descending | Select-Object Name, @{N='Memory(MB)';E={[math]::Round($_.WorkingSet/1MB,2)}}

Tasks

  • Find the top 10 processes by memory usage
  • List all running services, sorted alphabetically
  • Count how many services are in each status (Running/Stopped)
  • Find all .log files modified in the last 7 days
  • Calculate total size of all files in a directory
# Files modified in last 7 days $cutoff = (Get-Date).AddDays(-7) Get-ChildItem -Recurse | Where-Object { $_.LastWriteTime -gt $cutoff }

Related Visualizers

PowerShell Pipeline

Reflection

How does PowerShell's object pipeline differ from Bash's text-based piping? What are the advantages?

4

Output Formatting & Export

Intermediate

Control how data is displayed and export it to various formats for reporting.

Concept Overview

# Format cmdlets (display only, don't pipe further!) Get-Process | Format-Table Name, CPU, WorkingSet -AutoSize Get-Service | Format-List * Get-EventLog -LogName System -Newest 5 | Format-Wide Message # Export to files Get-Process | Export-Csv processes.csv -NoTypeInformation Get-Service | ConvertTo-Json | Out-File services.json Get-Process | ConvertTo-Html | Out-File report.html # Import data $data = Import-Csv processes.csv $json = Get-Content services.json | ConvertFrom-Json # Out cmdlets Get-Process | Out-File output.txt Get-Process | Out-GridView # Interactive GUI! Get-Process | Out-String | clip # Copy to clipboard
Format Cmdlets Are Terminal

Format-* cmdlets are for display only. Once you format, you can't pipe the object data further. Always format as the last step in your pipeline!

Tasks

  • Display processes in a table with only Name, Id, and CPU columns
  • Export all running services to a CSV file
  • Create an HTML report of system information
  • Import a CSV file and filter its contents
  • Use Out-GridView to interactively explore processes
System Info HTML Report

Get-ComputerInfo | Select-Object CsName, OsName, OsVersion | ConvertTo-Html | Out-File report.html

5

PowerShell Scripting

Intermediate

Write reusable scripts with control flow, functions, and parameters.

Concept Overview

# Conditional statements if ($value -gt 100) { Write-Host "High" } elseif ($value -gt 50) { Write-Host "Medium" } else { Write-Host "Low" } # Loops foreach ($item in $collection) { Write-Host $item } for ($i = 0; $i -lt 10; $i++) { Write-Host $i } # Functions function Get-DiskSpace { param( [string]$ComputerName = $env:COMPUTERNAME ) Get-WmiObject Win32_LogicalDisk -ComputerName $ComputerName | Where-Object DriveType -eq 3 | Select-Object DeviceID, @{N='Size(GB)';E={[math]::Round($_.Size/1GB,2)}}, @{N='Free(GB)';E={[math]::Round($_.FreeSpace/1GB,2)}} }

Tasks

  • Write a script that checks if a file exists and creates it if not
  • Create a function that converts bytes to human-readable format
  • Write a loop that processes all files in a folder
  • Create a script with command-line parameters
  • Add error handling with try/catch blocks
# Error handling try { $content = Get-Content "nonexistent.txt" -ErrorAction Stop } catch { Write-Warning "File not found: $_" } finally { Write-Host "Cleanup complete" }

Related Visualizers

PowerShell Scripting
6

System Administration Tasks

Advanced

Use PowerShell for real-world sysadmin tasks: services, processes, event logs, and scheduled tasks.

Concept Overview

# Service management Get-Service -Name "Spooler" Stop-Service -Name "Spooler" -Force Start-Service -Name "Spooler" Restart-Service -Name "Spooler" # Process management Get-Process -Name "notepad" Stop-Process -Name "notepad" -Force Start-Process "notepad.exe" -ArgumentList "file.txt" # Event logs Get-EventLog -LogName System -Newest 10 Get-EventLog -LogName Application -EntryType Error -After (Get-Date).AddDays(-1) # Scheduled tasks Get-ScheduledTask | Where-Object State -eq 'Ready' # System info Get-ComputerInfo | Select-Object CsName, OsName, OsArchitecture Get-WmiObject Win32_OperatingSystem | Select-Object LastBootUpTime

Tasks

  • List all services that start automatically but are currently stopped
  • Find processes consuming more than 50% CPU
  • Search event logs for errors in the last 24 hours
  • Create a system health report (disk space, memory, uptime)
  • Write a script to monitor a service and restart if stopped
# Service monitoring script skeleton $ServiceName = "Spooler" $Service = Get-Service -Name $ServiceName if ($Service.Status -ne 'Running') { Write-Warning "$ServiceName is not running! Attempting restart..." Start-Service -Name $ServiceName Write-Host "Service restarted." -ForegroundColor Green }

Related Visualizers

PowerShell Admin

Final Reflection

Design a PowerShell automation script for your daily workflow. What tasks would it automate? What parameters would it need?