diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index f21786a29ac..bdfa2675c34 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -2208,7 +2208,8 @@ protected override void NewItem( { if (symLinkExists) { - WriteError(new ErrorRecord(new IOException("NewItemIOError"), "NewItemIOError", ErrorCategory.ResourceExists, path)); + string message = StringUtil.Format(FileSystemProviderStrings.SymlinkItemExists, path); + WriteError(new ErrorRecord(new IOException(message), "SymLinkExists", ErrorCategory.ResourceExists, path)); return; } } diff --git a/src/System.Management.Automation/resources/FileSystemProviderStrings.resx b/src/System.Management.Automation/resources/FileSystemProviderStrings.resx index 8432c926706..def94134b72 100644 --- a/src/System.Management.Automation/resources/FileSystemProviderStrings.resx +++ b/src/System.Management.Automation/resources/FileSystemProviderStrings.resx @@ -336,4 +336,7 @@ Maximum size for drive has been exceeded: {0}. - \ No newline at end of file + + Cannot create symbolic link because the path {0} already exists. + + diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 0a237783e96..c94aeda3fcd 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -344,6 +344,9 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows" $link.LinkType | Should BeExactly "SymbolicLink" $link.Target | Should Be $nonFile } + It "New-Item emits an error when path to symbolic link already exists." { + { New-Item -ItemType SymbolicLink -Path $realDir -Value $symLinkToDir -ErrorAction Stop } | ShouldBeErrorId "SymLinkExists,Microsoft.PowerShell.Commands.NewItemCommand" + } It "New-Item can create a symbolic link to a directory" -Skip:($IsWindows) { New-Item -ItemType SymbolicLink -Path $symLinkToDir -Value $realDir Test-Path $symLinkToDir | Should Be $true