PowerShell Lesson 2: Files and Folders β€” Managing, Filtering and Bulk Renaming

PowerShell Lesson 2: Files and Folders β€” Managing, Filtering and Bulk Renaming featured illustration

πŸ“Ž This article includes 2 downloadable practice files ↓

⏱ 2 min readUpdated 11 November 2026

πŸ“˜ PowerShell Beginner Course Β· Lesson 2 of 6

In this article
  1. Basic Level: Creating, Listing and Filtering Files
  2. Intermediate Level: Batch Renaming and Regex Replacements
  3. Expert Level: High-Performance Directory Cleanups and Robocopy Bridging
  4. Summary of Key File System Cmdlets
  5. Continue learning

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.

Advertisement

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.

Advertisement
# 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'
}
πŸ’‘ Safety Tip: Always append -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)

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.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free Β· AI can be wrong

Leave a Comment