如何遵循powershell中的快捷方式

fal*_*ets 2 powershell symlink cd shortcut

在powershell中,您可以使用cd dir进入目录dir.

但是,如果dir是目录的快捷方式,cd dir并且cd dir.lnk都给出错误,则说该目录不存在.

那么我该如何遵循这条捷径呢?

(在Linux中cd dir 运行.在Windows中,我不知道)

Sag*_*pre 5

使用shell com-object,您可以获取目标路径,然后从那里执行您希望的操作. 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
Run Code Online (Sandbox Code Playgroud)