EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Rename-Item

PowerShell Rename-Item

Chirag Nagarekar
Article byChirag Nagarekar
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated June 17, 2023

PowerShell Rename - Item

Introduction to PowerShell Rename-Item

The rename-Item cmdlet in PowerShell renames items like files, folders, registry, certificates, etc. This cmdlet renames items but not their actual content or cannot move items to a different location.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax:

Rename-Item
[-Path] <String>
[-LiteralPath] <String>
[-NewName] <String>
[-Force] [-PassThru] [-Credential <PSCredential>] [-WhatIf] [-Confirm] [-UseTranscation] [<CommonParameters>]

Parameters in PowerShell Rename-Item

Following are the parameters for the PowerShell Rename-Item:

  • Path: Source path of the item. This path should be in a string format and must be identical. You can specify wildcard character (*), but the path should resolve only one item instead of multiple items.
  • NewName: Destination file name. By default, it will take the same source path; explicitly specifying different paths will generate an error. You cannot use a wildcard (*) character in a new name. It is possible to rename multiple files and extensions.
  • Force: Renames item names that cannot be changed. For files or folders which are read-only or hidden, this parameter allows them to rename forcefully. This parameter cannot change the constant alias or the variable.
  • PassThru: By default, Rename-Object doesn’t generate any output. This parameter allows the output to display in the console.
  • Credential: If your destination item is in a different domain or work group, you can use this parameter to connect the destination path with different credentials.
  • Whatif: This parameter is used to notify what operation will perform if this command runs without actually running the command.
  • LiteralPath: This parameter also specifies the path of an item to rename. No characters are interpreted as wildcard characters. If the path includes a unique character, they must be enclosed in a single quotation so PowerShell will not interpret any character as escape sequences.
  • Confirm: This parameter prompts the user before performing the rename operation on the item.
  • UseTranscation: This parameter includes the command in the active transaction. This parameter is valid only when the transaction is in progress.
  • <CommonParameters>: The below parameters are supported by common parameters. ErrorAction, ErrorVariable, WarningAction, WarningVariablem OutBuffer, PipelineVariable, and OutVariable.

Examples to Implement PowerShell Rename-Item

Below is the example of implementing in PowerShell Rename-Item:

Example #1 –  Rename-Item

Code:

Rename-Item D:\Temp\Putty.log -NewName Putty1.log

In the above example, when you don’t provide the destination path for the file to rename, it will, by default, take the same source path. You can also rename items without specifying –Newname parameter. To do so, provide the space between the old and new names. For example,

Rename-Item D:\Temp\Putty.log Putty1.log

You can also rename the folder, as shown below.

Rename-Item D:\Temp -NewName Temp1

Example #2 – Rename-Item with Passthru parameter

To check the command output in the console, use the -Passthru parameter.

Code:

Rename-Item D:\Temp\Putty.log -NewName Putty1.log -PassThru

Output:

PowerShell Rename-Item - 1

Providing a different destination path will generate an error because this cmdlet can’t perform the move operation.

Rename-Item D:\Temp\putty.log -NewName D:\putty1.log -PassThru

Output:

PowerShell Rename-Item - 2

Example #3 – Rename-ChildItem with Whatif Parameter

When you use the whatif parameter, it will show you the action command will perform without actually running the command.

Rename-Item D:\Temp\cars.xml -NewName cars1.xml -PassThru -WhatIf

Output:

 Whatif Parameter

Example #4 – Rename-ChildItem with Confirm parameter

This parameter will ask for user confirmation before renaming the item.

Code:

Rename-Item D:\Temp\cars.xml -NewName cars1.xml –Confirm

Output:

Confirm parameter

You can also provide the value $true or $false to confirm the parameter. When the $true value is provided, it will prompt for user confirmation. When your $false value is provided, it will not prompt for the user confirmation and directly rename the item, and this is the default option in the –confirm parameter.

Rename-Item D:\Temp\cars.xml -NewName cars1.xml -Confirm:$true

Rename-Item D:\Temp\cars.xml -NewName cars1.xml -Confirm:$false

Example #5 – Rename registry value

Like file rename, you can also rename registry values.

Code:

Rename-Item HKLM:\SYSTEM\CurrentControlSet\Control\test -NewName Test2

Rename-Item HKLM:\SECURITY\Policy\Data -NewName Data1

Example #6 – Rename multiple files extension with Rename-Item

To rename multiple files extension, you can use the below command to replace them.

Code:

PS C:\WINDOWS\system32> Get-ChildItem D:\Temp\* -Include *.html | Rename-Item -NewName {$_.Name -replace '.html','.htm'} -PassThru

To rename multiple files or folders, you need to use the loop.

Output:

Rename multiple files

Conclusion

Rename-Item is an excellent tool to rename single or multiple files, extensions, folders, registry, etc. But it cannot move or copy the files. For them, there are different cmdlets available.

Recommended Articles

This is a guide to PowerShell Rename-Item. Here we discuss the basic introduction, parameters, and examples to implement with appropriate codes and outputs. You can also go through our other related articles to learn more –

  1. Set Variable in PowerShell
  2. Variable in PowerShell
  3. Hashtable in PowerShell
  4. Examples to Implement PowerShell New-Item
ADVERTISEMENT
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
  • Blog as Guest
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Cyber Monday Reloaded Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW