We can make use of the Rename-Item cmdlet to rename files in PowerShell. You can find this cmdlet under Microsoft.PowerShell.Management module.
Syntax:
Rename-Item
-LiteralPath <String>
[-NewName] <String>
[-Force]
[-PassThru]
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
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
<#
Code2care.org 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

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Powershell,
- PowerShell: How to Check if a Module is Already Installed
- PowerShell: How to Write Output to a File
- PowerShell: Steps to Connect to connect to Exchange Online
- PowerShell Fix: Get-Help cannot find the Help files for this cmdlet on this computer
- PowerShell Traditional For Loop Example
- How to Run PowerShell Script as Administrator (Elevate)
- Update Powershell Using Command Line
- How to delete a file using PowerShell [Windows/macOS]
- PowerShell: Grep Command Alternative - Select-String
- PowerShell Concatenate String Examples
- How to add Sleep to PowerShell Script
- Connect Azure AD (Active Directory) for PowerShell
- How to Open PowerShell on Mac?
- List of PowerShell Function Commands for Mac
- How to Connect to Azure AD Account using PowerShell on Mac
- Download Google Chrome setup exe file using PowerShell
- Set Environment Variable in PowerShell for Mac
- How to switch to Powershell on Mac Terminal
- How to add sleep in Powershell Script
- PowerShell on Mac: The term get-service is not recognized as a name of a cmdlet, function, script file, or executable program
- How to Identify installed PowerShell version
- How to check PowerShell version
- How to install PowerShell on Mac using Brew
- PowerShell Switch Statement with Examples
- PowerShell ISE Alternative for Mac
More Posts:
- Find Hostname of Mac running on macOS Ventura 13 - MacOS
- How to backup a file in Linux/Unix - Linux
- How to add a Delay of a Few Seconds in Java Program - Java
- 7 Python Arithmetic Operators with Examples [Tutorial] - Python
- [fix] macOS Ventura - Python3 xcrun: error: invalid active developer path missing xcrun at CommandLineTools - MacOS
- Multiple Microsoft 365 Services Down (Outlook, Teams, SharePoint, OneDrive) - 25 January 2023 (Solved) - Microsoft
- Install Cygwin on Windows PC to learn Objective-C - Objective-C
- [fix] Error response from daemon: conflict unable to remove repository reference ubuntu container is using its referenced image - Docker