powershell - 如何在powershell中遵循快捷方式

标签 powershell symlink shortcut cd

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

但如果 dir是目录的快捷方式,cd dircd dir.lnk两者都给出错误,说该目录不存在。

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

(在 Linux cd dir 中有效。在 Windows 中,我不知道)

最佳答案

使用 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

关于powershell - 如何在powershell中遵循快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46276418/

相关文章:

powershell - 如何在Active Directory cmdlet上有效使用 `-Filter`参数?

powershell - 为变量转义 Powershell 中的所有密码特殊字符

C# ShortCut 路径修改

windows - 使用 "-noprofile"参数提升当前脚本

PowerShell-在特定范围内执行脚本 block

XAMPP 符号链接(symbolic link)禁止访问

node.js - 有没有办法确定 Node 中符号链接(symbolic link)的目标路径?

ios - Xcode- "The Argument Is Invalid"

Linux,是否有在终端中执行最后多行命令的快捷方式,而不是复制和粘贴

python - 有没有办法减少类中大量重复的代码