PowerShell Logo Small

Rename-Item



This is the built-in help made by Microsoft for the command 'Rename-Item', in PowerShell version 2 - as retrieved from Windows version 'Microsoft® Windows Vista™ Ultimate ' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Renames an item in a Windows PowerShell provider namespace.

SYNTAX


Rename-Item [-Path] <string> [-NewName] <string> [-Credential <PSCredential>] [-Force] [-PassThru] [-Confirm] [-WhatIf] [-UseTransaction] [<Commo
nParameters>]



Search powershellhelp.space

DESCRIPTION


The Rename-Item cmdlet changes the name of a specified item. This cmdlet does not affect the content of the item being renamed.

You cannot use Rename-Item to move an item, such as by specifying a path along with the new name. To move and rename an item, use the Move-Item c
mdlet.



<

RELATED LINKS

Online version: http://go.microsoft.com/fwlink/?LinkID=113382
about_Providers
Clear-Item
Invoke-Item
Move-Item
Rename-ItemProperty
Set-Item
New-Item
Remove-Item
Get-Item
Copy-Item

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

C:\PS>rename-item -path c:\logfiles\daily_file.txt -newname monday_file.txt



Description
-----------
This command renames the file daily_file.txt to monday_file.txt.








-------------------------- EXAMPLE 2 --------------------------

C:\PS>rename-item -path project.txt -newname d:\archive\old-project.txt

Rename-Item : Cannot rename because the target specified represents a path or device name.
At line:1 char:12
+ rename-item <<<< -path project.txt -newname d:\archive\old-project.txt
+ CategoryInfo : InvalidArgument: (:) [Rename-Item], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RenameItemCommand


C:\PS> move-item -path project.txt -destination d:\archive\old-project.txt
# Command succeeds



Description
-----------
This example shows that you cannot use the Rename-Item cmdlet to both rename and move an item. Specifically, you cannot supply a path for the val
ue of the NewName parameter, unless the path is identical to the path specified in the Path parameter. Otherwise, only a new name is permitted.

The first command uses the Rename-Item cmdlet to rename the project.txt file in the current directory to old-project.txt in the D:\Archive direct
ory. The result is the error shown in the output.

The second command shows the correct way to move and rename a file by using the Move-Item cmdlet. The Move-Item cmdlet lets you specify both a ne
w path and a new name in the value of its Destination parameter.








-------------------------- EXAMPLE 3 --------------------------

C:\PS>rename-item HKLM:\Software\MyCompany\Advertising -NewName Marketing



Description
-----------
This command uses the Rename-Item cmdlet to rename a registry key from Advertising to Marketing. When the command is complete, the key is renamed
, but the registry entries in the key are unchanged.








-------------------------- EXAMPLE 4 --------------------------

C:\PS>get-childItem *.txt | rename-item -newname { $_.name -replace '\.txt','.log' }



Description
-----------
This example shows how to use the Replace operator to rename multiple files, even though the NewName parameter does not accept wildcard character
s.

This command renames all of the .txt files in the current directory to .log.

The command uses a Get-ChildItem cmdlet to get all of the files in the current directory that have a .txt file name extension. Then, it uses the
pipeline operator (|) to send the resulting files to the Rename-Item cmdlet.

In the Rename-Item command, the value of the NewName parameter is a script block that is executed before the value is submitted to the NewName pa
rameter.

In the script block, the $_ automatic variable represents each file object as it comes to the command through the pipeline. The command uses the
dot format (.) to get the Name property of each file object. The Replace operator replaces the ".txt" file name extension of each file with ".log
".

Because the Replace operator works with regular expressions, the dot preceding "txt" is interpreted to match any character. To ensure that it mat
ches only a dot (.), it is escaped with a backslash character (\). The backslash character is not required in ".log" because it is a string, not
a regular expression.