WindowsPowerShell Rename Item

Viewed 514

When I attempt to rename a contracts folder to capitalize the contracts folder I'm receiving an error for renaming it. I've tried "rename-item contracts Contracts" as well. Anyone know if I can force the command or do i need to change my syntax?

PS C:\> rename-item -Path .\contracts\ -NewName .\Contracts\
rename-item : Cannot rename the specified target, because it represents a path or device name.
At line:1 char:1
+ rename-item -Path .\contracts\ -NewName .\Contracts\
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Rename-Item], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RenameItemCommand
3 Answers

Rename-Item is (and filenames in general in Windows are) not case sensitive.

You need to rename it to a temp file and then rename to desired name.

Two steps, as below,

Rename-Item -Path ".\contracts" -NewName ".\contracts_temp" -PassThru | Rename-Item -NewName ".\Contracts"

Abdullah Leghari's helpful answer provides an effective workaround, but it's worth stating that what you tried should work if you remove the trailing \ (and, optionally, the leading .\):

  • A trailing \ (or /) in the -NewName argument rightfully results in the error message you saw, because the -NewName argument by definition accepts a name only, not a path (however, a leading .\ by itself is quietly tolerated, even though it arguably shouldn't be either, for consistency).

  • However, in case-insensitive file systems (by default: Windows, macOS), it should be possible to rename a directory to a case variation of it, which currently only works with files.

In short:

Rename-Item -Path .\contracts\ -NewName Contracts

should work and, if the stated bug is fixed, will work in a future PowerShell (Core) version (but won't ever in Windows PowerShell, which will see no updates except critical bug fixes).

You need to remove the trailing "\" from the path parameter.

rename-item .\contracts .\Contracts

or

rename-item -Path .\contracts -NewName .\Contracts
Related