How to Rename a File in PowerShell Quickly and Easily
To rename a file in PowerShell, use the
Rename-Item cmdlet followed by the current file path and the new name. For example, Rename-Item -Path 'oldname.txt' -NewName 'newname.txt' changes the file name.Syntax
The basic syntax to rename a file in PowerShell uses the Rename-Item cmdlet with two main parameters:
- -Path: Specifies the current file path or name.
- -NewName: Specifies the new name for the file.
This command changes the file's name but keeps it in the same folder.
powershell
Rename-Item -Path <current-file-name> -NewName <new-file-name>Example
This example renames a file named report.txt to summary.txt in the current directory.
powershell
Rename-Item -Path 'report.txt' -NewName 'summary.txt' # After running, the file 'report.txt' will be renamed to 'summary.txt'.
Common Pitfalls
Common mistakes when renaming files in PowerShell include:
- Using incorrect file paths or names, causing errors.
- Trying to rename a file that is open or locked by another program.
- Not having permission to rename the file.
- Forgetting to include the file extension in the new name, which can cause confusion.
Always check the file exists and you have the right permissions before renaming.
powershell
## Wrong: Missing file extension in new name Rename-Item -Path 'data.csv' -NewName 'data' ## Right: Include full new name with extension Rename-Item -Path 'data.csv' -NewName 'data_backup.csv'
Quick Reference
| Parameter | Description |
|---|---|
| -Path | Current file path or name to rename |
| -NewName | New name for the file |
| -Force | Overwrite read-only or hidden files (use with caution) |
| -WhatIf | Shows what would happen without making changes |
Key Takeaways
Use Rename-Item with -Path and -NewName to rename files in PowerShell.
Always include the file extension in the new name to avoid confusion.
Check file existence and permissions before renaming.
Use -WhatIf to preview changes safely before renaming.
Avoid renaming files that are open or locked by other programs.