Repository navigation
Description
Steps to reproduce
Follow example 7 here: [ https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item?view=powershell-7
1. Create a folder eg. named "power"
2. In this (power) folder create an empty folder named "Notice"
3. In this (power) folder create a file named "Notice.txt"
4. As admin run this script:
New-Item -ItemType SymbolicLink -Path .\link -Target .\Notice
Expected behavior
Functioning link to Notice named "link"
Actual behavior
link not working
#Note
Working perfectly with absolute path and in cmd
cmd >mklink /d "./link2" "./Notice"
Environment data
PS C:\retus\test2Die\power problems> $psversiontable
Name Value
PSVersion 7.1.0-preview.3
PSEdition Core
GitCommitId 7.1.0-preview.3
OS Microsoft Windows 10.0.18362
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Activity
KalleOlaviNiemitalo commentedon May 25, 2020
I think it's the same bug as in #12785, i.e. caused by #8783, but more serious here because Windows supports relative symbolic links, unlike relative junctions.
As a workaround, start PowerShell in the desired directory, instead of using
Set-Locationwithin PowerShell.New-Item -ItemType SymbolicLinkwill then correctly create a relative symbolic link. This works with both directory symbolic links and file symbolic links.iSazonov commentedon May 26, 2020
I'd expect two different PRs.
PowerShell/src/System.Management.Automation/namespaces/FileSystemProvider.cs
Line 2364 in 1f252f8
KalleOlaviNiemitalo commentedon May 26, 2020
This seems a duplicate of #9127.
vexx32 commentedon May 26, 2020
Seems like it has the same cause, but a slightly different symptom, aye.
KalleOlaviNiemitalo commentedon Jun 26, 2020
Should retest this with v7.1.0-preview.4.
iSazonov commentedon Jun 26, 2020
The example works for me.
anthonyvdotbe commentedon Mar 7, 2021
This issue occurs for me (though not always, and I don't understand yet when it does/doesn't) with v7.1.2 on Windows 10.0.19042.0.
Edit: I can consistently reproduce this by launching a Windows Sandbox, installing PowerShell 7.1.2 in it, and running the following 2 commands:
darthwalsh commentedon Jul 6, 2023
The actual behavior was changed, to now produce an error on relative paths: #18251 #19409
darthwalsh commentedon Jan 9, 2024
For comparison. this is working on macOS:
If it also works on Windows then the issue should be closed
darthwalsh commentedon Jul 9, 2024
Theodor Heiselberg (@sukkergris) can you check if this still reproduces for you on latest pwsh? Tried on Windows and i'm seeing functional relative links
KalleOlaviNiemitalo commentedon Jul 9, 2024
It's still not working quite right:
Above, the New-Item cmdlet should have created a directory symbolic link, because
-Target targetrefers to an existing directory. It incorrectly created a file symbolic link instead.However, if I then start another PowerShell process in the directory and retry the operation with that, it works fine:
It seems that, when New-Item is checking whether the -Target is a file or a directory, in order to decide whether it should create a file symbolic link or a directory symbolic link, it resolves the target path against the directory in which PowerShell was started, rather than the directory in which the link is being created.
Universal-Invariant commentedon Apr 7, 2025
New-Item -Path "Z:\SP" -ItemType SymbolicLink -Value "\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy2\windows\system32\config"
fails with
New-Item : Cannot find path '\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy2\windows\system32\config' because it does
not exist.
New-Item -Path "Z:" -Name "SP" -ItemType SymbolicLink -Value FileSystem::$snapshotPath
also fails.
but
mklink /d Z:\SP "\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy2\windows\system32\config"
works
One can get at this by creating a shadow volume:
(I've played around with various "solutions" but nothing works while mklink works fine)