PowerShell Logo Small

Rename-Item



This is the built-in help made by Microsoft for the command 'Rename-Item', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' 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] [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore |
Suspend}] [-InformationVariable [<System.String>]] [-PassThru] [-Confirm] [-WhatIf] [-UseTransaction [<SwitchParameter>]] [<CommonParameters>]
Rename-Item [-NewName] <String> [-Credential [<PSCredential>]] [-Force] [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}]
[-InformationVariable [<System.String>]] [-PassThru] -LiteralPath <String> [-Confirm] [-WhatIf] [-UseTransaction [<SwitchParameter>]] [<CommonParameters>]



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 cmdlet.



<

RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/p/?linkid=293901
Clear-Item
Copy-Item
Get-Item
Invoke-Item
Move-Item
New-Item
Remove-Item
Rename-ItemProperty
Set-Item
about_Providers

REMARKS

<

Examples


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

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



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










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

PS C:\>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

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



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 value 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 directory. 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 new path and a new name in the
value of its Destination parameter.










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

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



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 --------------------------

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



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

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 parameter.

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 matches 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.