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.
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.
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.
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.
Okay, I found the solution: after trying with reopening the Powershell session, relogging in to the machine, doing a ipconfig /flushdns as well as /release and /renew I realised I had several Servers running in the environment, all using the same software, and all servers had that software connected to each other. After restarting all the servers, and stopping the software service on all running servers, I was able to correctly use both Rename-Item and Move-Item.
The sofware on the other servers were using the registry keys on my server, which prevented me from renaming them. However, since the parent key was not in use or locked by the software, I was allowed to create new keys in paralell to the one I was trying to rename. This is why it let the old key remain with the current name, but it was also creating a new key with the new name. Personally, I think that Powershell should fail already with the Rename-Item and not create a duplicate, but if it is as I suspect, Rename-Item is actually a Copy-Item followed by a Remove-Item (in this case, Copy works but the subsequent Remove fails).