PowerShell for Engineers: Files, Inventory, Backups and Safe Automation
PowerShell ships with every modern Windows PC and is the quickest way to automate repetitive admin on lab and office machines: listing what is installed, finding large or duplicate files, backing up data folders, and scheduling jobs. Its key difference from the old command prompt is that commands pass objects, not text, so filtering and sorting are reliable.
Key takeaways
- Commands are Verb-Noun cmdlets (Get-ChildItem, Copy-Item); Get-Help and Get-Command find what you need.
- The pipeline passes objects: Get-ChildItem | Where-Object Length -gt 1GB | Sort-Object Length.
- Find duplicates reliably by grouping on Get-FileHash, not on names or sizes alone.
- Use robocopy for robust backups and mirrors, with logs and retry limits.
- Work safely: test with -WhatIf, log with Start-Transcript, stop on errors with try/catch and -ErrorAction Stop, never delete before checking.
1. Basics
| Task | Command |
|---|---|
| Help on a command | Get-Help Get-ChildItem -Examples |
| Find commands | Get-Command *Item* |
| See object properties | Get-ChildItem | Get-Member |
| Filter | Where-Object { $_.Length -gt 100MB } |
| Sort and pick | Sort-Object Length -Descending | Select-Object -First 20 |
| Export | Export-Csv report.csv -NoTypeInformation |
| Allow local scripts (current user) | Set-ExecutionPolicy -Scope CurrentUser RemoteSigned |
2. Files: large, old and duplicate
# 20 largest files under D:\Data
Get-ChildItem D:\Data -Recurse -File -ErrorAction SilentlyContinue |
Sort-Object Length -Descending | Select-Object -First 20 FullName, @{n='MB';e={[math]::Round($_.Length/1MB,1)}}
# Files not modified for 2 years (report only)
$cut = (Get-Date).AddYears(-2)
Get-ChildItem D:\Data -Recurse -File | Where-Object LastWriteTime -lt $cut |
Export-Csv old_files.csv -NoTypeInformation
# Duplicates by content hash (size first, then hash only same-size files)
Get-ChildItem E:\ -Recurse -File | Group-Object Length | Where-Object Count -gt 1 |
ForEach-Object { $_.Group | Get-FileHash -Algorithm SHA256 } |
Group-Object Hash | Where-Object Count -gt 1 |
ForEach-Object { $_.Group | Select-Object Hash, Path } | Export-Csv duplicates.csv -NoTypeInformation
3. System inventory
$cs = Get-CimInstance Win32_ComputerSystem
$os = Get-CimInstance Win32_OperatingSystem
[pscustomobject]@{
Name = $cs.Name; Model = $cs.Model; RAM_GB = [math]::Round($cs.TotalPhysicalMemory/1GB,1)
OS = $os.Caption; Build = $os.BuildNumber; Arch = $os.OSArchitecture
} | Export-Csv inventory.csv -NoTypeInformation
# Installed programs (registry, faster and safer than Win32_Product)
$keys = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*',
'HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
Get-ItemProperty $keys | Where-Object DisplayName |
Select-Object DisplayName, DisplayVersion, Publisher | Sort-Object DisplayName |
Export-Csv installed.csv -NoTypeInformation
Get-PnpDevice -PresentOnly | Select-Object Class, FriendlyName, Status | Export-Csv devices.csv -NoTypeInformation
4. Backups with robocopy
# Mirror D:\TestData to a backup drive, retry once, log everything robocopy D:\TestData F:\Backup\TestData /MIR /R:1 /W:1 /FFT /NP /LOG+:F:\Backup\robocopy.log # /MIR deletes files at the destination that no longer exist at the source - check the path twice.
| Switch | Meaning |
|---|---|
| /E | Copy subfolders including empty ones |
| /MIR | Mirror (= /E + delete extras at destination) |
| /R:n /W:s | Retry count and wait between retries |
| /XO | Exclude older files (incremental) |
| /MT:16 | Multi-threaded copy |
| /L | List only — dry run |
| /LOG+:file | Append to a log file |
5. Scheduling
$act = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-NoProfile -File C:\Scripts\backup.ps1' $trg = New-ScheduledTaskTrigger -Daily -At 23:30 Register-ScheduledTask -TaskName 'Nightly data backup' -Action $act -Trigger $trg -RunLevel Highest
6. Safe habits
- Run destructive commands with
-WhatIffirst; review the list before removing it. Start-Transcriptat the top of scripts to keep a record of what ran.- Use
try { … } catch { … }with-ErrorAction Stopso failures are caught instead of scrolling past. - Move files to a "_to_delete" folder rather than deleting outright; delete later after checking.
- Keep scripts in version control with a header block (purpose, version, date, changes).
7. Calculators
Command builder
8. Self-test: check your understanding
Pick an answer to see the explanation. Numerical answers are accepted within the stated tolerance (±3 % unless noted).
Score
9. References and further reading
- Microsoft Learn, PowerShell documentation (about_Pipelines, about_Try_Catch_Finally, Get-FileHash, Get-CimInstance).
- Microsoft Learn, robocopy command reference.
- Jones, D. and Hicks, J., Learn PowerShell in a Month of Lunches, 4th ed., Manning, 2022.