如何在powershell中遵循快捷方式 [英] How to follow a shortcut in powershell

查看:49
本文介绍了如何在powershell中遵循快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In powershell, you use cd dir to go into the directory dir.

但是如果dir是一个目录的快捷方式,cd dircd dir.lnk都会报错,说是目录不存在.

But if dir is a shortcut to a directory, cd dir and cd dir.lnk both give an error, saying that the directory doesn't exist.

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

So how do I follow that shortcut?

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

(In Linux cd dir just works. In Windows, I've got no idea)

推荐答案

使用 shell com-object,您可以获得目标路径,然后从那里做您想做的事.Get-ShortcutTargetPath

Using the shell com-object, you can get the target path and from there, do what you wish. 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中遵循快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆