如何在 Power Shell 中遵循 Windows 快捷方式?

Mat*_*ten 11 windows-7 powershell shortcuts

我正在使用 powershell,并且在当前目录中有一个指向目标目录的快捷方式。我想将当前目录更改为快捷方式指向的目录。从逻辑上讲,我想做的是:

cd your-files-here.lnk
Run Code Online (Sandbox Code Playgroud)

并在那个点上结束。我得到的是:

Set-Location : Cannot find path 'your-files-here.lnk' because it does not exist.
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\pscx\Modules\CD\Pscx.CD.psm1:111 char:17
+                 Set-Location <<<<  $path -UseTransaction:$UseTransaction
    + CategoryInfo          : ObjectNotFound: (your-files-here.lnk:String) [Set-Location], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
Run Code Online (Sandbox Code Playgroud)

我试过

ii your-files-here.lnk
Run Code Online (Sandbox Code Playgroud)

但这会打开一个资源管理器窗口,而不是更改当前目录。

EBG*_*een 8

不幸的是,Windows 并没有让使用快捷方式变得容易。这应该有效:

$sh = New-Object -COM WScript.Shell
cd $sh.CreateShortcut('your-files-here.lnk').TargetPath
Run Code Online (Sandbox Code Playgroud)


Kev*_*nko 7

您可以将此添加到您的Microsoft.PowerShell_profile.ps1文件中。cd然后该命令将按需要工作。

remove-item alias:cd -force
function cd($target)
{
    if($target.EndsWith(".lnk"))
    {
        $sh = new-object -com wscript.shell
        $fullpath = resolve-path $target
        $targetpath = $sh.CreateShortcut($fullpath).TargetPath
        set-location $targetpath
    }
    else {
        set-location $target
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 此外,我注意到 PowerShell 自动完成功能在我键入“cd”后不适用于非目录,这意味着我必须自己键入 .lnk 文件的全名。 (2认同)