diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index b8d8e6526a..48cce0aeb7 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -2434,6 +2434,13 @@ namespace Microsoft.PowerShell.Commands bool exists = false; + // junctions require an absolute path + if (!Path.IsPathRooted(strTargetPath)) + { + WriteError(new ErrorRecord(new ArgumentException(FileSystemProviderStrings.JunctionAbsolutePath), "NotAbsolutePath", ErrorCategory.InvalidArgument, strTargetPath)); + return; + } + try { exists = GetFileSystemInfo(strTargetPath, out isDirectory) != null; diff --git a/src/System.Management.Automation/resources/FileSystemProviderStrings.resx b/src/System.Management.Automation/resources/FileSystemProviderStrings.resx index 6ffbce6b88..d23ff660f0 100644 --- a/src/System.Management.Automation/resources/FileSystemProviderStrings.resx +++ b/src/System.Management.Automation/resources/FileSystemProviderStrings.resx @@ -345,4 +345,7 @@ {0} of {1} ({2:0.0} MB/s) + + Creating a junction requires an absolute path for the target. + diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 5849d2ea9f..67cd1d1889 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -613,6 +613,17 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows" Test-Path $junctionToDir | Should -BeTrue } + It 'New-Item fails creating junction with relative path' -Skip:(!$IsWindows) { + try { + Push-Location $TestDrive + 1 > 1.txt + { New-Item -ItemType Junction -Path 2.txt -Target 1.txt -ErrorAction Stop } | Should -Throw -ErrorId "NotAbsolutePath,Microsoft.PowerShell.Commands.NewItemCommand" + } + finally { + Pop-Location + } + } + It 'New-Item can create hardlink with relative path' { try { Push-Location $TestDrive