Rename-Item

Rename an item, in a PowerShell provider namespace.

Syntax
      Rename-Item [-path] string[] [-newName] string [-force]
              [-passThru] [-credential PSCredential] [-whatIf]
                  [-confirm] [-UseTransaction] [CommonParameters]

Key
   -path string
       The path(s) to the item(s) to be renamed. Wildcards are permitted.

   -newName string 
       The new name for the item.

   -force
       Override restrictions that prevent the command from succeeding, apart
       from security settings. e.g. Force will create file path directories 
       or override a files read-only attribute, but will not change file permissions.

   -passThru 
        Pass the object created by Rename-Item along the pipeline. 

   -credentialPSCredential
       Use a credential to validate access to the file. Credential represents
       a user-name, such as "User64" or "Domain64\User64", or a PSCredential
       object, such as the one retrieved by using the Get-Credential cmdlet.
       If you type a user name, you will be prompted for a password.
       This parameter is not supported by any PowerShell core cmdlets or providers.
 
   -whatIf
       Describe what would happen if you executed the command without
       actually executing the command.

   -confirm
       Prompt for confirmation before executing the command.

   -UseTransaction
       Include the command in the active transaction.

Standard Aliases for Rename-Item: ren, rni

Rename-Item changes the name of an item, it does not affect the content of the item being renamed.
Rename-Item cannot be used to move an item. To move and rename an item, use Move-Item.

The -Newname parameter does not accept wildcards, but you can use a -replace expression to calculate the new name.

Examples

Rename a file:

PS C:\> rename-item -path c:\docs\dump.csv -newname Report.xls

Rename all .TXT files as .LOG files in the current directory:

PS C:\> get-childitem -Path *.txt | rename-item -NewName {$_.name -replace ".txt",".log"}

Rename all the files in the current directory, prefixing each with "Top Secret - ":

PS C:\> get-childitem | % { rename-item $_ "Top Secret - $_"}

Rename a registry key:

PS C:\> rename-item HKLM:\Software\SS64\Demo1 -NewName Test2

Rename all .zip files in the current directory, adding the date and time as a suffix:

PS C:\> Get-ChildItem *.zip | ForEach { Rename-Item -Path $_.FullName -NewName "$($_.DirectoryName)\$($_.BaseName)_$(Get-Date -F yyyy-MM-dd_HH-mm)$($_.Extension)" }

When renaming a very large number of files, you may prefer to calculate the date just once to ensure that all the files get the exact same date/time stamp:

PS C:\> $stamp = Get-Date -F yyyy-MM-dd_HH-mm
PS C:\> Get-ChildItem *.zip | ForEach { Rename-Item -Path $_.FullName -NewName "$($_.DirectoryName)\$($_.BaseName)_$stamp$($_.Extension)" }

Rename all the .MP3 files in the C:\Music\ folder, using the -F operator to prefix them with "Song" then a six digit number, incremented for each file:

$i = 0
Get-ChildItem -Path C:\music -Filter *.mp3 |
ForEach-Object {
   $extension = $_.Extension
   $newName = "Song {0:d6}{1}" -f  $i, $extension
   $i++
   Rename-Item -Path $_.FullName -NewName $newName
}

“It is the function of art to renew our perception. What we are familiar with we cease to see. The writer shakes up the familiar scene, and, as if by magic, we see a new meaning in it” - Anais Nin

Related PowerShell Cmdlets

Clear-item - Remove content from a variable or an alias.
Copy-Item - Copy an item from a namespace location.
Get-item - Return an object that represents an item in a namespace.
invoke-item - Invoke an executable or open a file (START).
Move-item - Move an item from one location to another.
New-item - Create a new item in a namespace.
Set-item - Set the value of a provider pathname.
Remove-item - Remove an item.
StampMe - Script to rename a file with the current Date/Time.
Equivalent bash command: mv - Move or rename files or directories.


 
Copyright © 1999-2024 SS64.com
Some rights reserved