2

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
Daniel
  • 4,792
  • 2
  • 7
  • 20

3 Answers3

3

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
  • 2,332
  • 3
  • 24
  • 40
  • +1 for an effective _workaround_, but it's worth noting that no workaround should be necessary, and indeed it isn't with _files_, only with _directories_. – mklement0 Jul 11 '21 at 00:09
  • P.S.: The `-NewName` argument by design accepts a _name_ only, not a _path_, and while the `.\ ` prefix happens to be accepted (unlike a _trailing_ `\ `), it's best not to include it. – mklement0 Jul 11 '21 at 00:26
  • Hi Abdullah your answer worked perfectly for me. Thank you for your time and consultation. – Jonathan Loop Jul 11 '21 at 16:33
3

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

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Hi Mklement0 thank you for your insight and clarification. How each OS (Linux, Windows, MacOS) works are questions I'm always curious about and try to resolve using guides before I ask a question on stack overflow ("Windows PowerShell, which will see no updates except critical bug fixes"). This will help me in the future when passing on your advice to other professionals in this trade. – Jonathan Loop Jul 11 '21 at 16:38
  • 1
    Glad to hear it, @JonathanLoop, but just to be clear: the cross-platform, install-on-demand [_PowerShell (Core)_](https://github.com/PowerShell/PowerShell/blob/master/README.md) edition (versions 6 and above, currently at 7.1) also runs on Windows, and it _will_ see improvements there. This contrasts with the legacy, ships-with-Windows _Windows PowerShell_ edition, whose latest and final version is 5.1, which will see critical bug fixes only. – mklement0 Jul 11 '21 at 18:49
0

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

rename-item .\contracts .\Contracts

or

rename-item -Path .\contracts -NewName .\Contracts
CHNguyen
  • 164
  • 1
  • 1
  • 7