Accessing UNC paths when using other PSProviders

In PowerShell, the default Provider is usually the filesystem: C: drive, D: drive, etc. This provider can also access UNC paths, i.e, file shares on your network: \\MyServer\MyShare\exampleFile.txt  When creating a script or working in the console you sometimes need to use different PSProviders to access other types of resources.  Examples of this are the Registry (HKLM:\, HKCU:\) or MECM/SCCM’s CMSite Provider.  Typically, you will switch to this PSProvider to use it.

When working in other providers, access to the filesystem only works for local files.  If you try to access a file on a UNC path, an error is returned stating it cannot find path because it does not exist.

When using Registry provider, Get-ChildItem does not work to access UNC paths.

You could change providers to C: drive, access the file, and then switch back.  However, there is a more efficient way to do this. By specifying the provider name in the file path, PowerShell will use this provider to access the object in question.  This allows you to directly access a file on a share without having to manually switch providers.

Get-ChildItem Microsoft.PowerShell.Core\FileSystem::"\\MyServer\MyShare\exampleFile.txt"
Specifying the providers allows access to UNC paths.

This also works when using the default Filesystem provider to access other providers.  

Get-ChildItem Microsoft.PowerShell.Core\Registry::HKLM\SOFTWARE\dotnet\
Specifying the provider allows access to Registry without having to change to the Registry drive.

This can also be accomplished by using simple drive notation.

Get-ChildItem HKLM:\SOFTWARE\dotnet
Using drive notation to access a registry item.

I hope you find this helpful.

Remember, a script a day keeps the work away!
~ian-mor

Leave a comment