在 powershell 中,您使用 cd dir
进入目录dir
.
但如果 dir
是目录的快捷方式,cd dir
和 cd 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/