How can I follow a windows shortcut in power shell?
Unfortunately, Windows does not make it easy to work with shortcuts. This should work:
$sh = New-Object -COM WScript.Shell
cd $sh.CreateShortcut('your-files-here.lnk').TargetPath
You may add this to your Microsoft.PowerShell_profile.ps1
file. The cd
command will then work as desired.
remove-item alias:cd -force
function cd($target)
{
if($target.EndsWith(".lnk"))
{
$sh = new-object -com wscript.shell
$fullpath = resolve-path $target
$targetpath = $sh.CreateShortcut($fullpath).TargetPath
set-location $targetpath
}
else {
set-location $target
}
}
Is the shortcut a necessity?
You could use a Windows link for this. See mklink /?
for more information on Windows links/junction points.