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.

briantist
  • 45,546
  • 6
  • 82
  • 127
yzorg
  • 4,224
  • 3
  • 39
  • 57
  • @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](https://technet.microsoft.com/en-us/library/Hh849795.aspx) (latest version, v5). – yzorg Aug 07 '15 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 07 '15 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 07 '15 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 07 '15 at 18:29

1 Answers1

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.

yzorg
  • 4,224
  • 3
  • 39
  • 57
  • 16
    For directories you can use `New-Item -ItemType Junction -Name System33 -Value c:\windows\system32` – Chris S Oct 22 '15 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 '18 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 '18 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: https://superuser.com/a/343079/418261 – Keith Oct 22 '18 at 12:46
  • 7
    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 '19 at 07:22
  • @Palec Feel free to update the answer to demonstrate other parameters (i.e. examples), but even better as a new answer! – yzorg Jan 30 '19 at 14:26
  • 1
    Related Microsoft docs on the matter, [Hard Links and Junctions](https://learn.microsoft.com/en-us/windows/win32/fileio/hard-links-and-junctions) – robquinn Jun 22 '20 at 12:51
  • @robquinn's Links to Win32 docs, not PowerShell scenarios (create hardlinks, remove, etc). – yzorg Jun 22 '20 at 15:15
  • 1
    Oh, how I LOVE that! The real advantage: This can handle unicode filenames with style. Mklink itself can unicode, but you have the problem transition between powershell and CMD, where such filenames usually get messed up. – Joachim Otahal Mar 26 '22 at 20:38