Connecting to HKU on a remote computer

I am trying to connect to HKU on a remote computer.

I have tried

Invoke-Command -ComputerName SomeRemoteComputer -ScriptBlock {New-PSDrive -Name HKU -PSProvider registry -root Registry::HKEY_USERS}

It appears to work when I attempt to connect to the new psdrive or look at the psdrive output it does not appear

Am I trying to do something that is not supported ?

Your code will create the psdrive on the remote computer not your local pc. If you want to use the psdrive the actions would need to be in your script block. Or is that what you are doing and getting no output?

I checked the psdrive of the remote computer it did not show. there and the code returns error
"Cannot find drive. A drive with the name ‘HKU’ does not exist.
+ CategoryInfo : ObjectNotFound: (HKU:String) [Get-ItemProperty], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand
+ PSComputerName : SomeRemoteComputer

When the results of the original script are run the provider does not show Registry
When i run the same command locally the provider shows Registry

So the desired result is to use get-itemproperty like the following example

Invoke-Command -ComputerName SomeRemoteComputer -ScriptBlock {Get-ItemProperty 'HKU:\S*\Software\Microsoft\Windows\CurrentVersion\WinTrust\Trust Providers\Software Publishing' |select -ExpandProperty pspath}

New-PSDrive -name HKU -PSProvider Registry -Root HKEY_USERS

edit: I just tried your original code and it works for me too, so something else must be up.

If I enter-pssession SomeRemoteComputer the new-psdrive works. That is not ideal as psremoting is not enabled on each of my assets that I am attempting to reach.

So, let’s be clear on how Invoke-Command works. Once it finishes running the command, it disconnects because you’re using an ad-hoc session. So it’s creating the PSDrive, and then closing the session, which removes the PSDrive.

Registry PSDrives are not persistent - they apply only to the current session.

You could instead create a New-PSSession. Pass that session to Invoke-Command, instead of using -ComputerName. That way, the first Invoke-Command would create the drive, and a subsequent Invoke-Command could refer to the drive, until the PSSession is closed.

Please read Don’s comments carefully as this may be more about understanding how remoting works then a issue with your code. I have verified that you can create the psdrive and use it withing the same invoke-command.

Invoke-Command -ComputerName SomeRemoteComputer -ScriptBlock {
    New-PSDrive -Name HKU -PSProvider registry -root Registry::HKEY_USERS
    cd HKU:

@Don and @Jonathan that makes it much more clear. I am grateful for the feedback and then leads me to a loaded question… Why did Microsoft decide that they would make hkcu and hlm but not make it default for the othr registry drives?

I can only assume they created the default drives to account for the most common customer needs and enable everyone else to create the custom drives they need.