Need to rename report-2024.pdf to report-2025.pdf for every file in a folder? Or swap spaces for hyphens across a whole directory? PowerShell handles this in a single line.

The One-Liner

Get-ChildItem -File | Rename-Item -NewName { $_.Name -replace "2024", "2025" }

This renames every file in the current directory, replacing the first match of 2024 with 2025 in the file name. Files that don’t contain 2024 are left alone.

Breaking it down:

  • Get-ChildItem -File — lists files only (no subdirectories).
  • Rename-Item -NewName { ... } — the script block receives each file as $_ and returns the new name.
  • $_.Name -replace "2024", "2025" — performs the substitution on the file name.

Practical Examples

Replace spaces with hyphens:

Get-ChildItem -File | Rename-Item -NewName { $_.Name -replace " ", "-" }

Replace a word in a specific file type only:

Get-ChildItem -File -Filter "*.pdf" | Rename-Item -NewName { $_.Name -replace "draft", "final" }

Preview Before Renaming

Add -WhatIf to do a dry run — PowerShell prints what it would rename without touching any files:

Get-ChildItem -File | Rename-Item -NewName { $_.Name -replace "2024", "2025" } -WhatIf

Output:

What if: Performing the operation "Rename File" on target "Item: C:\docs\report-2024.pdf Destination: C:\docs\report-2025.pdf".
What if: Performing the operation "Rename File" on target "Item: C:\docs\invoice-2024.pdf Destination: C:\docs\invoice-2025.pdf".

Always run with -WhatIf first.

Recursive — All Subdirectories

Add -Recurse to walk the entire directory tree:

Get-ChildItem -File -Recurse | Rename-Item -NewName { $_.Name -replace "2024", "2025" }

-replace Is Regex-Based

The -replace operator treats the first argument as a regular expression. For most literal text like years, words, or hyphens, this doesn’t matter — it just works.

Where it matters: regex metacharacters like ., (, ), [, $, and * have special meaning. If your search string contains any of these, escape them with a backslash:

# Replacing a literal dot (e.g. "v1.0" -> "v2.0")
Get-ChildItem -File | Rename-Item -NewName { $_.Name -replace "v1\.0", "v2.0" }

Without the backslash, v1.0 would match v1X0, v100, or anything where . matches any character.

For simple words, numbers, and spaces, no escaping is needed.