#PSTip Using UNC paths when working with PowerShell providers

When working with different PowerShell providers (PSProviders), the use of UNC paths can lead to error messages. This tip will show the pitfalls of using UNC paths when working with PowerShell providers other than the file system. For a full list of the PSProviders available on your system use the Get-PSProvider cmdlet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
PS C:\> Get-PSProvider
Name                 Capabilities                            Drives
----                 ------------                            ------
Alias                ShouldProcess                           {Alias}
Environment          ShouldProcess                           {Env}
FileSystem           Filter, ShouldProcess, Credentials      {C, D, E, Q...}
Function             ShouldProcess                           {Function}
Registry             ShouldProcess, Transactions             {HKLM, HKCU}
Variable             ShouldProcess                           {Variable}
Certificate          ShouldProcess                           {Cert}
WSMan                Credentials                             {WSMan}

The output shows the default PowerShell 2.0 providers. If you use PowerShell 3.0 or have imported, for example, ActiveDirectory or WebAdministration module, an output of your command will be different. PowerShell providers expose the PowerShell drives (PSDrive). We can change the current location to the Alias: PowerShell drive using the following command:

1
PS> Set-Location -Path Alias:

Use the Get-ChildItem Cmdlet to list the contents of the root of this drive. We can export the output to CSV file without a problem:

1
PS Alias:\> Get-ChildItem | Export-Csv -Path C:\Test.csv

However, when we attempt to do this by using a UNC path the command fails:

1
2
3
4
5
6
7
PS Alias:\> Get-ChildItem | Export-Csv -Path \\Server01\c$\TestUNC.csv

Export-Csv : Cannot open file because the current provider (Microsoft.PowerShell.Core\Alias) cannot open a file.
At line:1 char:30

+ Get-ChildItem -Path Alias: | Export-Csv -Path \\Server01\c$\TestUNC.csv
+ CategoryInfo          : InvalidArgument: (:) [Export-Csv], PSInvalidOperationException

There are two solutions to work around this issue. We can create a PSDrive that maps to the UNC path:

1
2
PS Alias:\> New-PSDrive -Name UNCPath -PSProvider FileSystem -Root \\Server01\c$\
PS Alias:\> Get-ChildItem | Export-Csv -Path UNCPath:\TestPSDrive.csv

Or explicitly state the PSProvider as shown in the following example:

1
2
3
PS Alias:\> Get-ChildItem | Export-Csv -Path Microsoft.PowerShell.Core\FileSystem::\\Server01\c$\TestUNC.csv
The Microsoft.PowerShell.Core\ part of this command can be omitted, shortening the command to:
PS Alias:\> Get-ChildItem | Export-Csv -Path FileSystem::\\localhost\c$\TestUNC.csv

For more information regarding PowerShell provider you can refer to the built-in help topic by running Get-Help about_Providers.

Share on: