Is it possible to test write permissions to the registry without actually writing? I have some code now where I just do the write, and exit with an error if it fails. But I am doing that after the user has approved processing and I would really rather do it during initialization, so if it's going to fail I never ask the user about proceeding, only to immediately exit with the failure. The other behavior I am looking for is to not actually change anything on the machine without that user approval.
I COULD do the write during init, then delete again, before getting user approval and then creating my key again, but that's ugly.
I had hoped -WhatIf
would work, but while
New-Item -Path:'registry::HKLM\SOFTWARE\Test'
throws an error when I am not running elevated,
New-Item -Path:'registry::HKLM\SOFTWARE\Test' -WhatIf
throws no error and in fact returns exactly the same message. Which is pretty stupid. I mean, basically it's Do-AThing -WhatIf
returns "Do a thing", whether it will actually succeed or fail. Well, no shit, Sherlock.
Anyway, is there any elegant option in PowerShell, or am I going to have to accept one of the ugly solutions?