Created
June 7, 2012 19:40
-
-
Save jpoehls/2891103 to your computer and use it in GitHub Desktop.
Native PowerShell wrapper for MKLINK.
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
function New-Symlink { | |
<# | |
.SYNOPSIS | |
Creates a symbolic link. | |
#> | |
param ( | |
[Parameter(Position=0, Mandatory=$true)] | |
[string] $Link, | |
[Parameter(Position=1, Mandatory=$true)] | |
[string] $Target | |
) | |
Invoke-MKLINK -Link $Link -Target $Target -Symlink | |
} | |
function New-Hardlink { | |
<# | |
.SYNOPSIS | |
Creates a hard link. | |
#> | |
param ( | |
[Parameter(Position=0, Mandatory=$true)] | |
[string] $Link, | |
[Parameter(Position=1, Mandatory=$true)] | |
[string] $Target | |
) | |
Invoke-MKLINK -Link $Link -Target $Target -HardLink | |
} | |
function New-Junction { | |
<# | |
.SYNOPSIS | |
Creates a directory junction. | |
#> | |
param ( | |
[Parameter(Position=0, Mandatory=$true)] | |
[string] $Link, | |
[Parameter(Position=1, Mandatory=$true)] | |
[string] $Target | |
) | |
Invoke-MKLINK -Link $Link -Target $Target -Junction | |
} | |
function Invoke-MKLINK { | |
<# | |
.SYNOPSIS | |
Creates a symbolic link, hard link, or directory junction. | |
#> | |
[CmdletBinding(DefaultParameterSetName = "Symlink")] | |
param ( | |
[Parameter(Position=0, Mandatory=$true)] | |
[string] $Link, | |
[Parameter(Position=1, Mandatory=$true)] | |
[string] $Target, | |
[Parameter(ParameterSetName = "Symlink")] | |
[switch] $Symlink = $true, | |
[Parameter(ParameterSetName = "HardLink")] | |
[switch] $HardLink, | |
[Parameter(ParameterSetName = "Junction")] | |
[switch] $Junction | |
) | |
# Ensure target exists. | |
if (-not(Test-Path $Target)) { | |
throw "Target does not exist.`nTarget: $Target" | |
} | |
# Ensure link does not exist. | |
if (Test-Path $Link) { | |
throw "A file or directory already exists at the link path.`nLink: $Link" | |
} | |
$isDirectory = (Get-Item $Target).PSIsContainer | |
$mklinkArg = "" | |
if ($Symlink -and $isDirectory) { | |
$mkLinkArg = "/D" | |
} | |
if ($Junction) { | |
# Ensure we are linking a directory. (Junctions don't work for files.) | |
if (-not($isDirectory)) { | |
throw "The target is a file. Junctions cannot be created for files.`nTarget: $Target" | |
} | |
$mklinkArg = "/J" | |
} | |
if ($HardLink) { | |
# Ensure we are linking a file. (Hard links don't work for directories.) | |
if ($isDirectory) { | |
throw "The target is a directory. Hard links cannot be created for directories.`nTarget: $Target" | |
} | |
$mkLinkArg = "/H" | |
} | |
# Capture the MKLINK output so we can return it properly. | |
# Includes a redirect of STDERR to STDOUT so we can capture it as well. | |
$output = cmd /c mklink $mkLinkArg `"$Link`" `"$Target`" 2>&1 | |
if ($lastExitCode -ne 0) { | |
throw "MKLINK failed. Exit code: $lastExitCode`n$output" | |
} | |
else { | |
Write-Output $output | |
} | |
} | |
Export-ModuleMember New-Symlink, New-Hardlink, New-Junction |
👍 I was just about to write this myself and stumbled over your nice module! Thanks for taking the time and sharing :)
Here's some SymbolicLink wrapper with .NET I wrote.
https://github.com/guitarrapc/PowerShellUtil/tree/master/SymbolicLink
@guitarrpc It's hard for me to use your scripts when all the files are named wrong (*-SyNbolicLink
). Your function names are correct though; a little faith restored. Without combing over your code, I can only assume there are other basic issues.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Link to article: An MKLINK PowerShell Module