diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index eca208ec5ad..172e8849570 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -4817,6 +4817,13 @@ protected override string GetParentPath(string path, string root) { parentPath = EnsureDriveIsRooted(parentPath); } +#if !UNIX + else if (parentPath.Equals(StringLiterals.DefaultPathSeparatorString, StringComparison.Ordinal)) + { + // make sure we return two backslashes so it still results in a UNC path + parentPath = "\\\\"; + } +#endif return parentPath; } // GetParentPath diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 909cc24de08..504d28a2bf4 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -1301,3 +1301,24 @@ Describe "Extended FileSystem Path/Location Cmdlet Provider Tests" -Tags "Featur } } } + +Describe "UNC paths" -Tags 'CI' { + It "Can Get-ChildItems from a UNC location using " -Skip:(!$IsWindows) -TestCases @( + @{cmdlet="Push-Location"}, + @{cmdlet="Set-Location"} + ) { + param($cmdlet) + $originalLocation = Get-Location + try { + $systemDrive = ($env:SystemDrive).Replace(":","$") + $testPath = Join-Path "\\localhost" $systemDrive + & $cmdlet $testPath + Get-Location | Should BeExactly "Microsoft.PowerShell.Core\FileSystem::$testPath" + $children = { Get-ChildItem -ErrorAction Stop } | Should Not Throw + $children.Count | Should BeGreaterThan 0 + } + finally { + Set-Location $originalLocation + } + } +}