
π This article includes 2 downloadable practice files β
In this article
File system automation is one of the most common tasks for system administrators and office data analysts. In PowerShell, files, folders, registry keys, and certificate stores are all represented through unified Providers, meaning the same cmdlets (Get-ChildItem, New-Item, Copy-Item, Remove-Item) work consistently across your disk, registry, and environment variables.
Basic Level: Creating, Listing and Filtering Files
The standard cmdlet for listing directory contents is Get-ChildItem (aliased as dir and ls). Unlike legacy CMD, it returns rich FileInfo and DirectoryInfo objects.
# List all Excel files in Documents, including subfolders
Get-ChildItem -Path ":USERPROFILEDocuments" -Filter *.xlsx -Recurse -File
# Filter files created in the last 7 days
= (Get-Date).AddDays(-7)
Get-ChildItem -Path "D:Reports" -File | Where-Object { .LastWriteTime -ge }
Intermediate Level: Batch Renaming and Regex Replacements
Renaming hundreds of files with consistent date formats or prefix patterns takes seconds using pipeline piping to Rename-Item.
# Bulk rename: Add a prefix '2026-Q4_' to all PDF invoice files
Get-ChildItem -Path "C:Invoices" -Filter *.pdf | ForEach-Object {
= "2026-Q4_" + .Name
Rename-Item -Path .FullName -NewName
}
# Advanced regex replacement: convert 'report_15_11_2026.csv' to '20261115_report.csv'
Get-ChildItem -Path "C:Data" -Filter *.csv | Rename-Item -NewName {
.Name -replace 'report_(d{2})_(d{2})_(d{4}).csv', '$3$2$1_report.csv'
}
-WhatIf to Rename-Item, Move-Item, or Remove-Item before running destructive commands. PowerShell will print exactly what it would modify without changing a single file!Expert Level: High-Performance Directory Cleanups and Robocopy Bridging
When dealing with enterprise file shares containing over 100,000 files, standard Get-ChildItem -Recurse can consume large amounts of memory because it builds an in-memory collection. For extreme scale, stream the file system directly using the .NET EnumerateFiles API or bridge into robocopy:
# High-speed .NET file enumeration (sub-millisecond streaming)
= "D:Archive"
[System.IO.Directory]::EnumerateFiles(, "*.log", [System.IO.SearchOption]::AllDirectories) | ForEach-Object {
= [System.IO.FileInfo]::new()
if (.LastWriteTime -lt (Get-Date).AddDays(-90)) {
Remove-Item -LiteralPath -Force
}
}
Summary of Key File System Cmdlets
| Cmdlet | Purpose | Common Parameters |
|---|---|---|
Get-ChildItem |
Lists directory contents | -Path, -Filter, -Recurse, -File, -Directory |
New-Item |
Creates files or directories | -ItemType Directory, -Force |
Copy-Item |
Copies items to new path | -Destination, -Recurse, -Container |
Move-Item |
Moves or relocates items | -Destination, -Force |
Rename-Item |
Modifies file/folder names | -NewName, -WhatIf |
Remove-Item |
Deletes files/folders | -Recurse, -Force, -WhatIf |
π Practice files for this article
β¬ Download all 2 files (ZIP Β· 569 B)
- π₯οΈps-02-files.ps1Lesson code file.β¬ PS1 Β· 462 B
- πps-02-files-output.txtOutput from our test run.β¬ TXT Β· 102 B
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.