Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

powershell

What is the Rename-Item cmdlet in PowerShell?

Gutha Vamsi Krishna

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

Overview

The Rename-Item cmdlet is used to rename a file with a new name.

Syntax

Rename-Item -Path "path of the old file" -NewName "new name of the file"

Parameters

  • Path: This is the path to the old file.
  • NewName: This is the new name to rename the file.

Return value

It doesn't return anything.

Example

In the following example, we rename the file abc.txt to def.txt using the cmdlet Rename-Item. We validate it by looking at filenames before and after the rename.

example.ps1
abc.txt
#!/usr/bin/pwsh -Command
Write-Host "------------List of files before renamig the file------------"
Get-ChildItem -Name
#rename the file
Rename-Item -Path "abc.txt" -NewName "def.txt"
Write-Host "------------List of files after renamig the file------------"
Get-ChildItem -Name

Explanation

  • Line 4: We display the files in the current directory using the cmdlet Get-ChildItem. Here, we can see that the file has the name abc.txt.
  • Line 7: We rename the file abc.txt to def.txt using the cmdlet Rename-item and pass the path as abc.txt and NewName as def.txt.
  • Line 10: We display the files in the current directory using the cmdlet Get-ChildItem. Here, we can see that the file name has been changed to def.txt.

RELATED TAGS

powershell

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring