31

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). Commented 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. Commented 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! Commented 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. Commented Aug 7, 2015 at 18:29

2 Answers 2

46
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.

Sign up to request clarification or add additional context in comments.

9 Comments

For directories you can use New-Item -ItemType Junction -Name System33 -Value c:\windows\system32
Another way to do directories is: New-Item -ItemType SymbolicLink -Name "SymLink Name" -Value "Real Path to Folder"
@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: 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
I recommend using -Path instead of -Name, because -Path may contain even path to the hardlink to be created, not only its filename.
|
1

something like this:

#remove-item $newHardLinkFullPath # if exist
New-Item -ItemType HardLink -path $newHardLinkFullPath -Value $realFileOrOldHardLinkFullPath

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.