Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to follow a shortcut in powershell

In powershell, you use cd dir to go into the directory dir.

But if dir is a shortcut to a directory, cd dir and cd dir.lnk both give an error, saying that the directory doesn't exist.

So how do I follow that shortcut?

(In Linux cd dir just works. In Windows, I've got no idea)

like image 688
falsePockets Avatar asked Sep 18 '17 09:09

falsePockets


People also ask

How do I run a shortcut in PowerShell?

One of the quickest ways to start PowerShell in any modern version of Windows is to use the Run window. A fast way to launch this window is to press the Win + R keys on your keyboard. Then, type powershell and press the Enter key or click OK.

What is shift enter in PowerShell?

To add a line, press Shift + Enter . You can add multiple lines. Each additional line begins with >> , the continuation prompt. Press Enter to execute the command.


1 Answers

Using the shell com-object, you can get the target path and from there, do what you wish. Get-ShortcutTargetPath

function Get-ShortcutTargetPath($fileName) {
    $sh = New-Object -COM WScript.Shell
    $targetPath = $sh.CreateShortcut($fileName).TargetPath 
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($sh) | Out-Null
    return $targetPath
}


$file = 'Path\to\Filename.lnk'
$TargetPath = Get-shortcutTargetPath($file)

if (Test-Path -PathType Leaf $TargetPath) {
    $TargetPath = Split-Path -Path $TargetPath
}

Set-Location $TargetPath
like image 121
Sage Pourpre Avatar answered Oct 01 '22 23:10

Sage Pourpre