28

How do I create file hardlink in PowerShell on Windows 10?

PSCX has New-Hardlink, but is it still needed on Windows 10?

You could always shell out to mklink, but from powershell that requires you prefix the command with cmd /c, which is ugly and hard to remember.

4
  • @briantist You added a tag to my question. Have you tried PowerShell v5 on downlevel OS to ensure that ItemType is present? The value isn't documented on the API docs for New-Item (latest version, v5).
    – yzorg
    Aug 7, 2015 at 12:20
  • I have not yet tried it on a downlevel OS, but it's unlikely to be tied to the OS. Junctions, hard links, and symbolic links have been supported in NTFS since (I think) its inception. One reason the documentation might not have info on it is because the *-Item cmdlets have dynamic parameters that are based on the PS Provider (in this the case the filesystem provider) so it doesn't always list all possible parameters. But I'm just speculating.
    – briantist
    Aug 7, 2015 at 17:35
  • @briantist Correction: I was asking whether -ItemType Hardlink was present in downlevel OSes. But, I have serious doubts, since the plumbing for .NET to call OS APIs changed significantly in Win 8.1 and Win 10. Just checking!
    – yzorg
    Aug 7, 2015 at 18:26
  • interesting, I'm not sure yet. WMF 5 roadmap was just released so it looks like we'll get a production ready version within the month that we can apply downlevel. I'll keep an eye out.
    – briantist
    Aug 7, 2015 at 18:29

1 Answer 1

39

New-Item -ItemType HardLink -Name CoverageCount.cs -Value ..\..\OPIAspnet5\Models\CoverageCount.cs

The ItemType parameter now includes a type for hardlinks, which I feel is a hidden gem of PowerShell on Windows 10.

9
  • 16
    For directories you can use New-Item -ItemType Junction -Name System33 -Value c:\windows\system32
    – Chris S
    Oct 22, 2015 at 22:02
  • 1
    Another way to do directories is: New-Item -ItemType SymbolicLink -Name "SymLink Name" -Value "Real Path to Folder"
    – Keith
    Oct 17, 2018 at 15:46
  • 1
    @KeithLammers SymbolicLink on Windows requires elevated privileges, which is why I don't recommend them and didn't include it. (It is very rare that I need to run something elevated these days, like once or twice per week.)
    – yzorg
    Oct 18, 2018 at 18:35
  • 1
    @yzorg: Good point! After looking further I also found out that they differ in how they're expanded. Junctions are expanded at the server and SymLinks at the client: superuser.com/a/343079/418261
    – Keith
    Oct 22, 2018 at 12:46
  • 5
    I recommend using -Path instead of -Name, because -Path may contain even path to the hardlink to be created, not only its filename.
    – Palec
    Jan 30, 2019 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.