Rename-Item

From $1

Synopsis

Renames an item in a Windows PowerShell provider namespace.

Syntax

Rename-Item [-PassThru] [-Credential [<PSCredential>]] [-Force] [-Path] [<string>] [-NewName] [<string>] [-confirm] [-whatIf] [<CommonParameters>]

Detailed Description

The Rename-Item cmdlet assigns 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.

Parameters

-Credential [<PSCredential>]

Specifies a user account that has permission to perform this action. The default is the current user.

Type a user-name, such as "User01" or "Domain01\User01", or enter a PSCredential object, such as one generated by the Get-Credential cmdlet. If you type a user name, you will be prompted for a password.

This parameter is not supported by any providers installed with Windows PowerShell

Required?   false
Position?   named
Default value?    
Accept pipeline input?   true (ByPropertyName)
Accept wildcard characters?   false

-Force [<SwitchParameter>]

Allows the cmdlet to override restrictions to renaming files as long as security is not compromised.

Required?   false
Position?   named
Default value?    
Accept pipeline input?   false
Accept wildcard characters?   false

-NewName [<string>]

Specifies the new name of the item. Enter only a name, not a path and name. If you enter a path, Rename-Item considers it to be an error. To rename and move an item, use the Move-Item cmdlet.

You cannot use wildcard characters in the value of NewName. To specify a name for multiple files, use the -Replace operator in a regular expression. For more information about -Replace, type "get-help about_comparison_operators". For a demonstration, see the examples.

Required?   true
Position?   2
Default value?    
Accept pipeline input?   true (ByPropertyName)
Accept wildcard characters?   false

-PassThru [<SwitchParameter>]

Passes an object representing the item to the pipeline. By default, this cmdlet does not generate any output.

Required?   false
Position?   named
Default value?    
Accept pipeline input?   false
Accept wildcard characters?   false

-Path [<string>]

Specifies the path to the item to rename.

Required?   true
Position?   1
Default value?    
Accept pipeline input?   true (ByPropertyName)
Accept wildcard characters?   false

Input Type

 

Return Type

None or an object representing the renamed item.

Notes

For more information, type "Get-Help Rename-Item -detailed". For technical information, type "Get-Help Rename-Item -full".

When specifying multiple values for a parameter, use commas to separate the values. For example, "<parameter-name> <value1>, <value2>".

Examples

EXAMPLE 1

PS> 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> rename-item -path project.txt -newname d:\archive\old-project.txt
move-item -path project.txt -destination d:\archive\old-project.txt

 

This example shows that you cannot use the Rename-Item cmdlet to both rename and move an item. Specifically, you cannnot supply a path for the value of the NewName parameter. 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> 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 completes, the key is renamed, but the registry entries in the key are unchanged.

EXAMPLE 4

PS> 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" and "log" is interpreted to match any character. To insure that it matches only a dot (.), it is escaped with a backslash character (\).

Tags:
 
Images (0)
 
Comments (0)
You must login to post a comment.

 
SourceForge.net