Rename-Item

Renames an item in a PowerShell provider namespace.

语法

ByPath (默认值)

Rename-Item
    [-Path] <String>
    [-NewName] <String>
    [-Force]
    [-PassThru]
    [-Credential <PSCredential>]
    [-WhatIf]
    [-Confirm]
    [-UseTransaction]
    [<CommonParameters>]

ByLiteralPath

Rename-Item
    [-NewName] <String>
    -LiteralPath <String>
    [-Force]
    [-PassThru]
    [-Credential <PSCredential>]
    [-WhatIf]
    [-Confirm]
    [-UseTransaction]
    [<CommonParameters>]

说明

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

You can't use Rename-Item to move an item, such as by specifying a path together with the new name. To move and rename an item, use the Move-Item cmdlet.

示例

Example 1: Rename a file

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

Rename-Item -Path "c:\logfiles\daily_file.txt" -NewName "monday_file.txt"

Example 2: Rename and move an item

You can't use Rename-Item to both rename and move an item. Specifically, you can't 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.

This example attempts 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.

Rename-Item -Path "project.txt" -NewName "d:\archive\old-project.txt"
Rename-Item : can't 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], PS>  Move-Item -Path "project.txt" -De
stination "d:\archive\old-project.txt"

Example 3: Rename a registry key

This example renames 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.

Rename-Item -Path "HKLM:\Software\MyCompany\Advertising" -NewName "Marketing"

Example 4: Rename multiple files

This example renames all the *.txt files in the current directory to *.log.

Get-ChildItem *.txt
    Directory: C:\temp\files

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        10/3/2019   7:47 AM           2918 Friday.TXT
-a----        10/3/2019   7:46 AM           2918 Monday.Txt
-a----        10/3/2019   7:47 AM           2918 Wednesday.txt
Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace '.txt','.log' }
Get-ChildItem *.log
    Directory: C:\temp\files

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        10/3/2019   7:47 AM           2918 Friday.log
-a----        10/3/2019   7:46 AM           2918 Monday.log
-a----        10/3/2019   7:47 AM           2918 Wednesday.log

The Get-ChildItem cmdlet gets all the files in the current folder that have a .txt file extension then pipes them to Rename-Item. The value of NewName is a script block that runs 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 script block uses the -replace operator to replace the file extension of each file with .log. Notice that matching using the -replace operator is not case sensitive.

参数

-Confirm

Prompts you for confirmation before running the cmdlet.

参数属性

类型:SwitchParameter
默认值:False
支持通配符:False
不显示:False
别名:cf

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Credential

Note

This parameter is not supported by any providers installed with PowerShell. To impersonate another user, or elevate your credentials when running this cmdlet, use Invoke-Command.

参数属性

类型:PSCredential
默认值:Current user
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-Force

Forces the cmdlet to rename items that can't otherwise be changed, such as hidden or read-only files or read-only aliases or variables. The cmdlet can't change constant aliases or variables. Implementation varies from provider to provider. For more information, see about_Providers.

Even using the Force parameter, the cmdlet can't override security restrictions.

参数属性

类型:SwitchParameter
默认值:False
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-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. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell PowerShell not to interpret any characters as escape sequences.

For more information, see about_Quoting_Rules.

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False
别名:PSPath

参数集

ByLiteralPath
Position:Named
必需:True
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-NewName

Specifies the new name of the item. Enter only a name, not a path and name. If you enter a path that differs from the path that is specified in the Path parameter, Rename-Item generates an error. To rename and move an item, use Move-Item.

You can't use wildcard characters in the value of the NewName parameter. To specify a name for multiple files, use the Replace operator in a regular expression. For more information about the Replace operator, see about_Comparison_Operators.

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:1
必需:True
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-PassThru

Returns an object that represents the item to the pipeline. By default, this cmdlet does not generate any output.

参数属性

类型:SwitchParameter
默认值:False
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Path

Specifies the path of the item to rename.

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False

参数集

ByPath
Position:0
必需:True
来自管道的值:True
来自管道的值(按属性名称):True
来自剩余参数的值:False

-UseTransaction

Includes the command in the active transaction. This parameter is valid only when a transaction is in progress. For more information, see about_Transactions.

参数属性

类型:SwitchParameter
默认值:False
支持通配符:False
不显示:False
别名:usetx

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.

参数属性

类型:SwitchParameter
默认值:False
支持通配符:False
不显示:False
别名:wi

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

输入

String

You can pipe a string that contains a path to this cmdlet.

输出

None or an object that represents the renamed item.

This cmdlet generates an object that represents the renamed item, if you specify the PassThru parameter. Otherwise, this cmdlet does not generate any output.

备注

Rename-Item is designed to work with the data exposed by any provider. To list the providers available in your session, type Get-PsProvider. For more information, see about_Providers.