
π This article includes 1 downloadable practice file β
PowerShell ships with every copy of Windows, and most office workers never open it. That is a pity, because the jobs that eat an afternoon β renaming 400 scanned invoices, merging 30 CSV exports, finding which folder is filling the disk β are one-liners here.
In this article
- 1. Go to a folder and look around
- 2. Rename files in bulk
- 3. Combine many CSV files into one
- 4. Add the source file name as a column
- 5. Find duplicate files
- 6. Which folder is eating the disk?
- 7. Clear Downloads older than 60 days
- 8. Search inside text files
- 9. List what is installed
- 10. Save a script and run it again
- When not to use PowerShell
- Continue learning
Open it from the Start menu (type PowerShell). Everything below is safe to try on a copy of your files first. Commands that change things have a -WhatIf switch that shows what would happen without doing it β use it.
1. Go to a folder and look around
Set-Location "D:\Scans\March"
Get-ChildItem | Sort-Object Length -Descending | Select-Object -First 10 Name, Length
The ten biggest files in the folder. Get-ChildItem is the dir you know; the pipe | hands its output to the next command.
2. Rename files in bulk
Get-ChildItem *.pdf | Rename-Item -NewName { $_.Name -replace 'Scan_', 'Invoice_' } -WhatIf
$_ means βthe current fileβ. Remove -WhatIf when the preview looks right. To add a date prefix instead:
Get-ChildItem *.pdf | Rename-Item -NewName { $_.LastWriteTime.ToString('yyyy-MM-dd') + ' ' + $_.Name }
3. Combine many CSV files into one
Get-ChildItem .\exports\*.csv | ForEach-Object { Import-Csv $_ } | Export-Csv .\combined.csv -NoTypeInformation
Headers are handled for you: each file is read as rows of objects, and one header is written at the end. This only works if the files share the same column names. If one export says βAmountβ and another βAmtβ, fix that first.
4. Add the source file name as a column
Get-ChildItem .\exports\*.csv | ForEach-Object {
$file = $_.Name
Import-Csv $_ | Select-Object *, @{ n = 'SourceFile'; e = { $file } }
} | Export-Csv .\combined.csv -NoTypeInformation
Now you can tell which branch or month each row came from β the first thing anyone asks when a total looks odd.
5. Find duplicate files
Get-ChildItem D:\Photos -Recurse -File |
Get-FileHash |
Group-Object Hash | Where-Object Count -gt 1 |
ForEach-Object { $_.Group.Path; '' }
A hash is a fingerprint of the file’s contents, so two files with different names but identical content are caught. The script only lists them; delete by hand once you have looked.
6. Which folder is eating the disk?
Get-ChildItem C:\Users\$env:USERNAME -Directory | ForEach-Object {
$size = (Get-ChildItem $_.FullName -Recurse -File -ErrorAction SilentlyContinue | Measure-Object Length -Sum).Sum
[pscustomobject]@{ Folder = $_.Name; GB = [math]::Round($size / 1GB, 2) }
} | Sort-Object GB -Descending
Usually the answer is Downloads, a forgotten video export, or an OST file from Outlook.
7. Clear Downloads older than 60 days
Get-ChildItem "$env:USERPROFILE\Downloads" -File |
Where-Object LastWriteTime -lt (Get-Date).AddDays(-60) |
Remove-Item -WhatIf
8. Search inside text files
Select-String -Path .\logs\*.txt -Pattern 'ERROR' | Select-Object Filename, LineNumber, Line
The Windows version of grep. Works on CSV and log files too.
9. List what is installed
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion | Sort-Object DisplayName
Handy when IT asks βwhich version of Office do you have?β.
10. Save a script and run it again
Paste any of these into Notepad and save as combine.ps1. The first time, Windows may refuse to run scripts. Allow scripts you wrote yourself for your own user only:
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
Then run it with .\combine.ps1 from the folder. Pair it with Task Scheduler and the job runs every morning without you.
When not to use PowerShell
If the job lives inside Excel β formatting, formulas, pivot tables β VBA or Power Query is the better tool. PowerShell is best for files, folders and anything outside a single workbook. For a comparison of options, see types of automation.
π Practice files for this article
- π₯οΈAll code from this post11 examples in one file, ready to open in your editor.β¬ PS1 Β· 2 KB
Free to use for learning. Files with macros (.bas) are plain text β import them with Alt+F11 β File β Import File, and always test on a copy.
Stuck on a step? Ask a question and the AI answers using this article.