PowerShell Rename-Item | PowerShell Rename file
The PowerShell Rename-Item cmdlet renames an item in a PowerShell provider namespace. It changes the name of a specified item. It does not affect the content of an item being renamed. The ri, rm, rmdir, del, erase, and rd are the aliases of the Rename-Item cmdlet.
Rename-Item [-Path <string>] [-NewName <string>] [-Force] [-PassThru] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [<CommonParameters>]
Rename-Item [-NewName] <string> -LiteralPath <string> [-Force] [-PassThru] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [<CommonParameters>]
The -Path parameter is used to specify the path of an item to rename.
The -LiteralPath parameter is used to specify a path to one or more locations. Its value is used exactly as it is typed. If the path includes the escape characters, enclose it in single quotation marks. Single quotation mark tells the Windows PowerShell that it should not interpret any character as an escape sequence. There is no character in the cmdlet, which is interpreted as a wildcard.
The -NewName parameter is used to specify the new name of an item. If a user enters a path that differs from the path specified in the -Path parameter, this parameter generates an error. We cannot use wildcard characters in the value of the-NewName parameter.
The -Force parameter forces the cmdlet to rename those items which cannot be changed, such as hidden, read-only files, aliases, or variables. It cannot remove constant variables or aliases. Even using the -Force parameter, the cmdlet cannot override security permissions.
The -PassThru parameter returns an object which represents an item to the pipeline. By default, it does not generate any output.
The -WhatIf parameter displays what would happen if the cmdlet runs. The cmdlet does not execute.
The -Confirm parameter prompts a confirmation before running the cmdlet.
Example 1: Rename a file
The cmdlet, in this example, renames the file s.txt to c.txt in the current directory.
Example 2: Rename multiple files
The cmdlet, in this example, renames all the .txt files to .doc files.
The Get-ChildItem cmdlet is used in this example to get all the files in the current folder that have a .txt file extension, then pipes them to Rename-Item cmdlet. The value of -NewName is a script block that runs before the value is submitted to the -NewName parameter.