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)
但这会打开一个资源管理器窗口,而不是更改当前目录。
不幸的是,Windows 并没有让使用快捷方式变得容易。这应该有效:
$sh = New-Object -COM WScript.Shell
cd $sh.CreateShortcut('your-files-here.lnk').TargetPath
Run Code Online (Sandbox Code Playgroud)
您可以将此添加到您的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)
| 归档时间: |
|
| 查看次数: |
8483 次 |
| 最近记录: |