Last active
August 29, 2015 14:07
-
-
Save mzs-github/09df37aabd89149c3831 to your computer and use it in GitHub Desktop.
HardLink.psm1 - create hardlinks in native PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adapted from http://learn-powershell.net/2013/07/18/create-a-hard-link-using-powershell/ | |
Set-StrictMode -Version Latest | |
$ErrorActionPreference = "Stop" | |
Add-Type @" | |
using System; | |
using System.ComponentModel; | |
using System.Runtime.InteropServices; | |
namespace mklink | |
{ | |
public class hardlink | |
{ | |
// SetLastError: tell the .NET runtime to capture the last Win32 | |
// error if it fails | |
[DllImportAttribute("Kernel32.dll", SetLastError = true)] | |
public static extern bool CreateHardLink(string lpFileName,string lpExistingFileName,IntPtr lpSecurityAttributes); | |
// Wrapper for CreateHardLink() that throws an exception on failure. | |
// Done in C# rather than in PowerShell to ensure PowerShell | |
// internals don't clobber the error. | |
public static void CreateHardLinkOrThrow(string lpFileName,string lpExistingFileName,IntPtr lpSecurityAttributes) | |
{ | |
if (!CreateHardLink(lpFileName, lpExistingFileName, lpSecurityAttributes)) { | |
throw new System.ComponentModel.Win32Exception(); | |
} | |
} | |
} | |
} | |
"@ | |
Function New-HardLink { | |
<# | |
.SYNOPSIS | |
Creates a hard link to a file | |
.DESCRIPTION | |
Creates a hard link to a file as an alternative to mklink.exe | |
.PARAMETER Path | |
Name of the file that you will reference with a hard link. | |
.PARAMETER HardLink | |
Name of the hard link to create. Can be a full path/unc or just the name. | |
If only a name is given, the hard link will be created on the current directory that the | |
function is being run on. | |
.NOTES | |
Name: New-HardLink | |
Author: Boe Prox | |
Created: 15 Jul 2013 | |
.EXAMPLE | |
New-HardLink -Path "C:\users\admin\downloads\file.txt" -HardLink "C:\users\admin\desktop\file" | |
HardLink Target | |
------- ------ | |
C:\Users\admin\Desktop\file C:\Users\admin\Downloads\file.txt | |
Description | |
----------- | |
Creates a hard link to file.txt called file on the desktop. | |
#> | |
[cmdletbinding( | |
SupportsShouldProcess=$True | |
)] | |
Param ( | |
[parameter(Position=0,ValueFromPipeline=$True, | |
ValueFromPipelineByPropertyName=$True,Mandatory=$True)] | |
[ValidateScript({ | |
If (Test-Path $_ -PathType Leaf) {$True} Else { | |
Throw "`'$_`' doesn't exist or isn't a file!" | |
} | |
})] | |
[string]$Path, | |
[parameter(Mandatory=$True)] | |
[string]$HardLink | |
) | |
Process { | |
If ($PScmdlet.ShouldProcess($Path,'Create Hard Link')) { | |
[mklink.hardlink]::CreateHardLinkOrThrow($HardLink,$Path,[intptr]::Zero) | |
$object = New-Object PSObject -Property @{ | |
HardLink = $HardLink | |
Target = $Path | |
} | |
$object.pstypenames.insert(0,'System.File.HardLink') | |
$object | |
} | |
} | |
} | |
Export-ModuleMember -Function New-HardLink |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment