#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:

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:

PS C:\> 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:

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:

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:

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:

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.

Filed in: Columns, Tips and Tricks Tags: , ,

5 Responses to "#PSTip Using UNC paths when working with PowerShell providers"

  1. Kazun says:

    PS C:Userstest> dir | export-csv \wks-8C$ex.csv
    PS C:Userstest> dir \wks-8C$ex.csv

    Directory: \wks-8C$

    Mode LastWriteTime Length Name
    —- ————- —— —-
    -a— 07.11.2012 10:24 4861 ex.csv

    PS C:Users> $env:COMPUTERNAME

    PowerShell V 2

  2. Barry Uche Ajokubi says:

    this was useful for me. thanks

  3. ManuD says:

    Thank you. The only way Test=Path with UNC share worked was your way –
    Test-Path -Path Microsoft.PowerShell.Core\FileSystem::$filepath

    where $filepath was \\server\share\subfolder\test.txt

Leave a Reply

Submit Comment

© 2017 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: