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

2

29

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.

Borderer answered 6/8, 2015 at 18:30 Comment(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).Borderer
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.Laflam
@Laflam 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!Borderer
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.Laflam
B
42

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.

Borderer answered 6/8, 2015 at 18:31 Comment(9)
For directories you can use New-Item -ItemType Junction -Name System33 -Value c:\windows\system32Bujumbura
Another way to do directories is: New-Item -ItemType SymbolicLink -Name "SymLink Name" -Value "Real Path to Folder"Madonnamadora
@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.)Borderer
@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/418261Madonnamadora
I recommend using -Path instead of -Name, because -Path may contain even path to the hardlink to be created, not only its filename.Harold
@Harold Feel free to update the answer to demonstrate other parameters (i.e. examples), but even better as a new answer!Borderer
Related Microsoft docs on the matter, Hard Links and JunctionsNea
@robquinn's Links to Win32 docs, not PowerShell scenarios (create hardlinks, remove, etc).Borderer
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.Encephalic
L
1

something like this:

#remove-item $newHardLinkFullPath # if exist
New-Item -ItemType HardLink -path $newHardLinkFullPath -Value $realFileOrOldHardLinkFullPath
Lawry answered 16/9, 2023 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.