1

Is it possible to create a broken symbolic link (meaning destination does not exist when link is created) using PowerShell on Windows 11 and on NTFS filesystem?

And if so will it mimic UNIX behavior so that when destination later gets created the link will become valid?

I tried New-Item -ItemType SymbolicLink ... without and with -Force and that scriptlet won't allow me to create link to non-existent destination with error: New-Item : Cannot find path '<path>' because it does not exist.

I know it is possible to create valid link and then remove its destination which renders the link dangling, but I am asking specifically about creating broken link from scratch.

blami
  • 632

1 Answers1

0

I haven't found a way to do it entirely within PowerShell, but the "mklink" command built-in to CMD can do it. One of the responses to this Microsoft Community post shows how to call that from PowerShell. They pass parameters as a comma separated list but it is also possible to do it as a single string:

start-process -FilePath $env:comspec -ArgumentList "/k mklink /j $LinkPath $MissingTargetPath"

Just be sure to quote paths containing spaces and escape those quotes with backticks so PowerShell interprets it all as one string.

This fails with no error message when attempted in a remote PowerShell session (and likely in a script) because /k tries to open a separate CMD window and fails. Switch it to /c and it works.

Run mklink /? in a CMD session for details, including the need to pass /d if the target is a folder.

benrifkah
  • 162