Last active
April 25, 2023 14:46
-
-
Save Katzenwerfer/fe4c4ca867a7a71047507342de5f7ae7 to your computer and use it in GitHub Desktop.
Wrapper for the MKLINK command from the legacy cmd.exe terminal
This file contains hidden or 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-Link { | |
[Alias("mklink")] | |
param( | |
[ValidateSet( | |
"SymbolicLink", | |
"HardLink", | |
"DirectoryLink", | |
"Junction" | |
)] | |
[string] | |
$LinkType = "SymbolicLink", | |
[Parameter( | |
Mandatory, | |
Position = 0, | |
HelpMessage = "Name of the link." | |
)] | |
[string] | |
$Name, | |
[Parameter( | |
Mandatory, | |
Position = 1, | |
ValueFromPipeline = $true, | |
HelpMessage = "Path to a file." | |
)] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
[System.IO.FileInfo]$Target, | |
[switch] | |
$PrintVars | |
) | |
if ($PrintVars) { | |
Write-Host "LinkType = |$LinkType|" -ForegroundColor Red | |
Write-Host "Name = |$Name|" -ForegroundColor Red | |
Write-Host "Target = |$Target|" -ForegroundColor Red | |
Write-Host "DebugPreference = $DebugPreference" -ForegroundColor Red | |
} | |
switch ($LinkType) { | |
"SymbolicLink" { cmd.exe /C MKLINK $Name $Target } | |
"HardLink" { cmd.exe /C MKLINK /H $Name $Target } | |
"DirectoryLink" { cmd.exe /C MKLINK /D $Name $Target } | |
"Junction" { cmd.exe /C MKLINK /J $Name $Target } | |
Default { | |
Write-Warning "The specified type is not recognized." | |
Write-Warning "Defaulting to file symbolic link." | |
cmd.exe /c MKLINK $Name $Target | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment