如何遵循powershell中的快捷方式

问题描述 投票:2回答:1

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

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

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

(在Linux中cd dir正常工作。在Windows中,我不知道)

powershell symlink shortcut cd
1个回答
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

0
投票

在Windows 10 cd directory_name或cd dir *中,如果您只想要部分名称,则假定没有其他目录以相同的dir *开头。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.