Powershell "UNC paths are not supported" but I'm using Push-Location

3.5k views Asked by At

I am running a command line program in powershell working on our \servername\files folder as the running folder. I get a "UNC paths are not supported" error even though I am using Push-Location (which is the only solution I find while googling). The simplest code that gives me this error is as follows:

Push-Location \\servername\files
cmd.exe \c ping 10.1.10.10
Pop-Location
3

There are 3 answers

0
Matt McNabb On BEST ANSWER

PetSerAl is correct, you're getting this response from cmd, not Powershell. This would work if you had a PSDrive configured first, but I don't know if that is very efficient for your use case:

New-PSDrive -Name S -PSProvider FileSystem -Root \\servername\files -Persist
Push-Location
Set-Location S:\
cmd.exe /c ping 10.1.1.1
Pop-Location
Get-PSDrive S | Remove-PSDrive
0
Steven LaFavor On

Push-Location will save where you are, but since you have not changed your location to a non-UNC location to execute the cmd.exe program, it complains.

Just put a Set-Location before the cmd.exe call to make sure that you are not on a UNC path. Something like:

Set-Location $env:APPDATA

would work.

0
postanote On

*-Location works as designed, as I'll demo below.

Demo ---

PS C:\Scripts> (Get-CimInstance -ClassName CIM_OperatingSystem).Caption
Microsoft Windows Server 2012 R2 Standard

PS C:\Scripts> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      -1     -1      



PS C:\Scripts> Test-Path -Path '\\FileServer01\Data'
True

PS C:\Scripts> $pwd

Path
----
C:\Scripts



PS C:\Scripts> Push-Location -Path '\\FileServer01\Data'

PS Microsoft.PowerShell.Core\FileSystem::\\FileServer01\Data> $pwd

Path
----
Microsoft.PowerShell.Core\FileSystem::\\FileServer01\Data 

As for this...

cmd.exe \c ping 10.1.10.10

... is not running from the UNC, that is running from your host, the fact that you pushd to a share is meaningless.

If a share has an exe you want to run there is no reason to change to that location to run it. Just use the defined methods to run external commands (see the link below) via PowerShell and just use the UNC path without switching there first. Even running a remote exe still means is using cmd.exe from your host.

PS Microsoft.PowerShell.Core\FileSystem::\\FileServer01\Data> Pop-Location

PS C:\Scripts> $pwd

Path
----
C:\Scripts



PS C:\Scripts> 

Running external commands, always require special consideration.

PowerShell: Running Executables https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

Solve Problems with External Command Lines in PowerShell https://devblogs.microsoft.com/scripting/solve-problems-with-external-command-lines-in-powershell

Top 5 tips for running external commands in Powershell https://powershelleverydayfaq.blogspot.com/2012/04/top-5-tips-for-running-external.html

Using Windows PowerShell to run old command line tools (and their weirdest parameters) https://blogs.technet.microsoft.com/josebda/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters

Execution of external commands in PowerShell done right https://mnaoumov.wordpress.com/2015/01/11/execution-of-external-commands-in-powershell-done-right https://mnaoumov.wordpress.com/2015/03/31/execution-of-external-commands-native-applications-in-powershell-done-right-part-2 https://mnaoumov.wordpress.com/2015/04/05/execution-of-external-commands-native-applications-in-powershell-done-right-part-3

Quoting specifics

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules https://trevorsullivan.net/2016/07/20/powershell-quoting