powershell rename file is the Rename-Item cmdlet that renames a file or folder without moving it. Use:
Rename-Item -Path "C:report.txt" -NewName "report_backup.txt" -WhatIf
Syntax
Rename-Item [-Path] <String> [-NewName] <String> [-Force] [-PassThru] [-Credential <PSCredential>] [-WhatIf] [-Confirm] [<CommonParameters>]
Rename-Item -LiteralPath <String> [-NewName] <String> [-Force] [-PassThru] [-Credential <PSCredential>] [-WhatIf] [-Confirm] [<CommonParameters>]
# Alias: ren, rni
Get-Alias -Definition Rename-Item
Tested on Windows 10 22H2 with PowerShell 7.4.
Options and Flags
| Flag | Type | Default | Description |
|---|---|---|---|
-Path |
String | (none) | Specifies the current path of the item (file or folder). Accepts wildcards. |
-LiteralPath |
String | (none) | Path with wildcard characters treated literally. Cannot accept wildcards. |
-NewName |
String | (required) | New name for the item (name only, not a full path). |
-Force |
SwitchParameter | false | Allows renaming of read‑only or hidden items. Overrides confirmation prompts unless set by policy. |
-PassThru |
SwitchParameter | false | Passes the renamed item object down the pipeline. |
-Confirm |
SwitchParameter | false | Prompts for confirmation before executing the rename. |
-WhatIf |
SwitchParameter | false | Shows what would happen if the command runs; the cmdlet is not executed. |
-Credential |
PSCredential | none | Account with permission to rename the item (commonly used for remote or registry items). |
Usage Examples
Example 1: Rename a single file
Rename-Item -Path "C:Reportssales_2024.csv" -NewName "sales_2024_backup.csv" -WhatIf
# Remove -WhatIf to execute
Renames the file in the same directory. The -WhatIf switch displays the intended change without modifying the file system. Always run with -WhatIf to verify the target file exists and the new name is correct. This cmdlet does not affect the content of the item being renamed.
Example 2: Bulk rename – change file extension
Get-ChildItem -Path "C:Logs*.log" | ForEach-Object {
Rename-Item -Path $_.FullName -NewName ($_.Name -replace '.log$', '.txt') -WhatIf
}
Changes the extension of all .log files to .txt. The -replace operator uses regex; the $ anchor ensures only the final extension is matched. Note that -replace is not case sensitive. This method preserves the file name and location. After verification, remove -WhatIf.
Example 3: Bulk rename – sanitise filenames
Get-ChildItem -Path "C:Books*.pdf" -Recurse | ForEach-Object {
$newName = $_.Name
$newName = $newName -replace "[()']", '' # remove brackets and apostrophes
$newName = $newName -replace "s+", '-' # replace spaces with hyphen
$newName = $newName -replace '-+', '-' # collapse multiple hyphens
$newName = $newName.ToLower()
Rename-Item -Path $_.FullName -NewName $newName -WhatIf
}
Cleans up book PDFs from publishers that contain special characters and irregular spacing. The script processes all subdirectories (-Recurse), removes parentheses and apostrophes, normalises spaces and hyphens, and forces lowercase. Always preview with -WhatIf.
Troubleshooting & Common Errors
| Error Message | Root Cause | Resolution Command |
|---|---|---|
Cannot find path '...' because it does not exist. |
The file path is missing or mistyped. | Test-Path "C:pathtofile.txt" to verify; correct the path. |
Access to the path '...' is denied. |
File is read‑only, hidden, or protected by ACL. | Add -Force parameter; if still denied, run PowerShell as Administrator. |
The input cannot be bound to any parameters of the command. |
Pipeline input is incorrectly formatted (e.g., passing full path instead of name for -NewName). |
Ensure -NewName receives only the filename, not the full path. Use $_.Name inside the script block. |
Rename-Item : Cannot rename because item at '...' does not exist. |
Rename-Item is executed on a non‑existent item. | Use Get-ChildItem to retrieve existing files before renaming. |
Rename-Item : Illegal characters in path. |
New name contains characters not allowed by Windows ( / : * ? " < > |). |
Filter out illegal characters: $newName -replace '[\/:*?"<>|]', '_' |
Frequently Asked Questions
What is the difference between `Rename-Item -NewName` and `Move-Item -Destination` for renaming a file?
Answer: Rename-Item changes the name within the same directory; Move-Item changes the path (rename + relocation).
Rename-Item only alters the file name, keeping it in the source folder. Move-Item can rename and move to another location. Use -WhatIf to preview. Example:
Rename-Item -Path "C:log.txt" -NewName "log_old.txt"
Move-Item -Path "C:log.txt" -Destination "D:archivelog_archived.txt"
When should I use the `-PassThru` flag with `Rename-Item`?
Answer: Use `-PassThru` when you need the renamed file object in a pipeline for further processing.
Without this flag, Rename-Item returns no output. With `-PassThru`, it emits the renamed file as a System.IO.FileInfo object. Common in automation:
Rename-Item -Path "*.tmp" -NewName { $_.Name -replace '.tmp$','.bak' } -PassThru | Set-ItemProperty -Name IsReadOnly -Value $true
How do I fix “Access to the path ‘C:file.txt’ is denied” when renaming a file?
Answer: Run PowerShell as Administrator, use `-Force` flag, or unlock file handles.
Common causes: file in use, insufficient privileges, or read-only attribute. Resolve by:
# Force rename even if read-only (requires modify permission)
Rename-Item -Path "C:file.txt" -NewName "file_renamed.txt" -Force
# Find locked processes (Sysinternals handle.exe or PowerShell)
Get-Process | Where-Object { $_.Modules.FileName -like "*file.txt*" }
Does `Rename-Item` work on Linux or macOS in PowerShell Core 7?
Answer: Yes, `Rename-Item` works cross-platform in PowerShell 7+ on Linux and macOS, using the file system provider.
Syntax is identical, but path separators differ. On Linux, use forward slashes or backslashes (automatically normalized). Example:
# Linux
Rename-Item -Path "/home/user/report.log" -NewName "report_old.log"
# macOS
Rename-Item -Path "/Users/user/data.csv" -NewName "data_backup.csv"
What is the fastest way to rename multiple files by pattern in PowerShell?
Answer: Use `Get-ChildItem` piped to `Rename-Item` with a script block in `-NewName` for regex replacements.
One-liner using the provider’s built-in rename with `-NewName { $_.Name -replace … }`. For bulk prefix/suffix:
# Add prefix "archive_" to all .log files
Get-ChildItem "*.log" | Rename-Item -NewName { "archive_" + $_.Name }
# Replace spaces with underscores using regex
Get-ChildItem "* *.txt" | Rename-Item -NewName { $_.Name -replace ' ', '_' }

Command Line Expert & Software Engineer
Welcome! I’m Thomas Heinrich, a software engineer and system administrator with a deep passion for the Command Line Interface (CLI). With years of experience navigating the terminal, building backend architectures, and automating server deployments, I created this space to share practical, real-world terminal knowledge.
Whether you are a beginner taking your first steps in a Linux environment or a seasoned DevOps engineer looking to optimize your deployment scripts, you will find actionable solutions here. My goal is to help you ditch the mouse, speed up your workflow, and harness the full power of the command line.