How to rename a file using PowerShell

We can make use of the Rename-Item cmdlet to rename files in PowerShell. You can find this cmdlet under Microsoft.PowerShell.Management module.


      -LiteralPath <String>
      [-NewName] <String>
      [-Credential <PSCredential>]

Let's take a look at a few examples,

Example 1: One-Liner

PS> Rename-Item ./data_2023_draft.csv -NewName ./data_2023.csv 

Rename with Path

PS> Rename-Item -Path D:/files/data_2023_draft.csv -NewName D:/files/data_2023.csv

Example 2: Rename a file using Script

<# PowerShell Examples

   Script to rename a file. 


$oldFileName = Read-Host "Enter File Name with Absolute Path To Rename "

$newFileName = Read-Host "Enter a new name for the file "

Rename-Item -Path $oldFileName -NewName $newFileName
PS> Enter File Name with Absolute Path To Rename: /Users/c2ctechtv/Desktop/file1.txt

Enter a new name for the file: data.txt

Rename a file using PowerShell Script


Facing issues? Have Questions? Post them here! I am happy to answer!

Author: Rakesh
Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap