Can PowerShell 1.0 create hard and soft links analogous to the Unix variety?
If this isn\'t built in, can someone point me to a site that has a ps1 script that mimi
I combined two answers (@bviktor and @jocassid). It was tested on Windows 10 and Windows Server 2012.
function New-SymLink ($link, $target)
{
if ($PSVersionTable.PSVersion.Major -ge 5)
{
New-Item -Path $link -ItemType SymbolicLink -Value $target
}
else
{
$command = "cmd /c mklink /d"
invoke-expression "$command ""$link"" ""$target"""
}
}
You can use this utility:
c:\Windows\system32\fsutil.exe create hardlink
Windows 10 (and Powershell 5.0 in general) allows you to create symbolic links via the New-Item cmdlet.
Usage:
New-Item -Path C:\LinkDir -ItemType SymbolicLink -Value F:\RealDir
Or in your profile:
function make-link ($target, $link) {
New-Item -Path $link -ItemType SymbolicLink -Value $target
}
Turn on Developer Mode to not require admin privileges when making links with New-Item
:
I found this the simple way without external help. Yes, it uses an archaic DOS command but it works, it's easy, and it's clear.
$target = cmd /c dir /a:l | ? { $_ -match "mySymLink \[.*\]$" } | % `
{
$_.Split([char[]] @( '[', ']' ), [StringSplitOptions]::RemoveEmptyEntries)[1]
}
This uses the DOS dir command to find all entries with the symbolic link attribute, filters on the specific link name followed by target "[]" brackets, and for each - presumably one - extracts just the target string.
In Windows 7, the command is
fsutil hardlink create new-file existing-file
PowerShell finds it without the full path (c:\Windows\system32) or extension (.exe).