PowerShell Rename-Item Cmdlet
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Clear-Item cmdlet in PowerShell.
https://cloudaffaire.com/powershell-clear-item-cmdlet/
In this blog post, we will discuss Rename-Item cmdlet in PowerShell. You can use Rename-Item cmdlet to rename a registry key name, file name or directory name in PowerShell. Rename-Item cmdlet only renames the item but does not changes it path. If you want to rename the item and change its path, use Move-Item cmdlet.
Rename-Item Cmdlet Syntax:
1 2 3 4 5 6 7 8 9 10 |
## Rename-Item ## [-Path] ## [-NewName] ## [-Force] ## [-PassThru] ## [-Credential ## [-WhatIf] ## [-Confirm] ## [-UseTransaction] ## [ |
Rename-Item Cmdlet Argument List:
- –Confirm: Prompts you for confirmation before running the cmdlet.
- –Credential: To impersonate another user or elevate your credentials.
- –Force: Forces the cmdlet to remove items that cannot otherwise be changed, such as hidden or read-only files or read-only aliases or variables.
- –LiteralPath: Specifies a path to one or more locations. The value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards.
- –Path: Specifies a path of the items being removed. Wildcard characters are permitted.
- –PassThru: Returns an object that represents the item to the pipeline. By default, this cmdlet does not generate any output.
- –NewName: Specifies the new name of the item. Enter only a name, not a path and name.
- –UseTransaction: Includes the command in the active transaction. This parameter is valid only when a transaction is in progress.
- –WhatIf: Shows what would happen if the cmdlet runs. The cmdlet is not run.
PowerShell Rename-Item Cmdlet:
Rename A Registry Key In PowerShell:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
####################################### ## PowerShell | Cmdlet | Rename-Item ## ####################################### ## PowerShell Latest Version (5) ## create a registry key and some files and directories New-Item -Path HKCU:\Software -Name MyRegKey -Value "Hello World" New-Item -ItemType Directory -Path "C:\MyDir\MyDir1","C:\MyDir\MyDir2", "C:\MyDir\MyDir3" New-Item -ItemType File -Path "C:\MyDir\MyFile1","C:\MyDir\MyFile2","C:\MyDir\MyFile3" ## rename a registry key in PowerShell Rename-Item -Path HKCU:\Software\MyRegKey -NewName MyNewRegKey Get-Item -Path HKCU:\Software\MyNewRegKey |
Rename A Directory In PowerShell:
1 2 3 4 5 |
## rename a directory in PowerShell Get-ChildItem -Path C:\MyDir Rename-Item -Path C:\MyDir\MyDir1 -NewName MyNewDir1 Get-ChildItem -Path C:\MyDir |
Rename Multiple Directories At Once In PowerShell:
1 2 3 4 5 |
## rename multiple directories at once in PowerShell Get-ChildItem C:\MyDir\ Get-ChildItem C:\MyDir\MyDir* | Rename-Item -NewName { $_.Name -replace 'MyDir','MyNewDir' } Get-ChildItem C:\MyDir\ |
Rename A File In PowerShell:
1 2 3 4 5 |
## rename a file in PowerShell Get-ChildItem -Path C:\MyDir Rename-Item -Path C:\MyDir\MyFile1 -NewName MyNewFile1 Get-ChildItem -Path C:\MyDir |
Rename Multiple Files At Once In PowerShell:
1 2 3 4 5 6 7 8 9 10 |
## rename multiple files at once in PowerShell Get-ChildItem C:\MyDir\ Get-ChildItem C:\MyDir\MyFile* | Rename-Item -NewName { $_.Name -replace 'MyFile','MyNewFile' } Get-ChildItem C:\MyDir\ ## delete items used in this demo Remove-Item -Path HKCU:\Software\MyNewRegKey Remove-Item -Path C:\MyDir -Recurse |
Hope you have enjoyed this article. In the next blog post, we will discuss Get-Item cmdlet in PowerShell.
To get more details on PowerShell, kindly follow below official documentation