I have been scratching my head at this for hours now. I'm guessing that this might be related to either permissions on the account I'm running Powershell with (unlikely as I'm logged in to the server with an account that is both local administrator as well as domain admin), or that the key is somehow used by another process. I looked at this Stack post as well as this Reddit thread but for some reason this only works for empty keys that I create for testing purposes, not the existing ones. There seems to be others having similar issues too without any success.
Test that works
I created a registry key called HKLM:\Software\MyTestKey, and then I run:
Get-Item 'HKLM:\Software\MyTestKey'
This returns:
Hive: HKEY_LOCAL_MACHINE\SOFTWARE
Name Property
---- --------
MyTestKey
So then I run:
Rename-Item 'HKLM:\Software\MyTestKey' -NewName "MyNewTestKey"
This runs correctly and I can verify that the key MyTestKey has been renamed to MyNewTestKey. This also works if the registry key has spaces in it.
Test that doesn't work
I then turn to an existing key for a software that I have installed. The service for that software is stopped. I run
Get-Item 'HKLM:\Software\MySoftware\KeyToChangeName'
This returns correctly:
Hive: HKEY_LOCAL_MACHINE\SOFTWARE\MySoftware
Name Property
---- --------
KeyToChangeName
However, when I run
Rename-Item 'HKLM:\Software\MySoftware\KeyToChangeName' -NewName "MyNewKeyname"
This throws an error:
Rename-Item : The registry key at the specified path does not exist.
At line:1 char:1
+ Rename-Item 'HKLM:\SOFTWARE\MySoftware\KeyToChangeName...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (HKEY_LOCAL_MACH...ame|MyDomainName:String) [Rename-Item], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.RenameItemCommand
The strangest part about this is that instead of renaming the key, Powershell actually creates a copy of the key, so now I have both KeyToChangeName as well as MyNewKeyname.
Commands tried so far
- Rename-Item 'HKLM:\Software\MySoftware\KeyToChangeName' -NewName "MyNewKeyname"
- Rename-Item 'HKLM:\Software\MySoftware\KeyToChangeName' -NewName MyNewKeyname
- Rename-Item "HKLM:\Software\MySoftware\KeyToChangeName" -NewName "MyNewKeyname"
- Rename-Item "HKLM:\Software\MySoftware\KeyToChangeName" -NewName MyNewKeyname
All of these results in the same error being thrown, but it still creates a copy of the key, not renaming it. I also tried Move-Item with both the -Path and -LiteralPath flags for all of the above commands but with the same results.
Why would powershell tell me that the key doesn't exist when it clearly doesn't? And even stranger, why would it create a new registry key with the same content as the old one, essentially duplicating it, instead of renaming it?
Any help is appreciated.