使用 powershell 获取快捷方式 (.lnk) 文件的目标 [英] Get target of shortcut (.lnk) file with powershell

查看:270
本文介绍了使用 powershell 获取快捷方式 (.lnk) 文件的目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆 .lnk 文件,需要根据快捷方式指向的目标对它们进行不同的处理.我几乎没有发现如何使用其他语言进行此操作,但没有发现使用 powershell 进行此操作.

I have a bunch of .lnk files and need to treat them differently depending on the target that the shortcut points to. I've found very little of how to this with other languages, but nothing about doing this with powershell.

我已经试过了:

$sh = New-Object -COM WScript.Shell
$target = $sh.CreateShortcut('<path>').Target

但是这会返回一个空字符串,即使我可以在 .lnk 属性中看到指定了目标.

But this returns an empty string even though I can see in the .lnk properties that the Target is specified.

知道如何做到这一点吗?

Any idea on how to accomplish this?

推荐答案

您在属性中犯了错误;正如 wOxxOm 建议的那样,您应该使用 TargetPath 而不是 Target:

You have made an error in the property; as wOxxOm suggests, you should be using TargetPath rather than Target:

$sh = New-Object -ComObject WScript.Shell
$target = $sh.CreateShortcut('<full-path-to-shortcut>').TargetPath

Google 和 MSDN 在这方面确实很有帮助;此外,将对象传递给 Get-Member 通常很有用且具有教育意义.这个问题还展示了如何使用 PowerShell 操作快捷方式,并使用相同的技术如此处所示.

Google and MSDN were indeed helpful here; additionally, piping objects to Get-Member can often be useful and educational. This question also shows how to manipulate shortcuts using PowerShell, and uses the same technique as seen here.

如果您还需要可执行文件的参数,则将它们单独存储:

If you want the arguments to the executable as well, those are stored separately:

$arguments = $sh.CreateShortcut('<full-path-to-shortcut>').Arguments

同样,将对象传递给 Get-Member - 在这种情况下,由 WScript.Shell.CreateShortcut() 返回的对象 - 提供了有用的信息.

Again, piping objects to Get-Member - in this case, the object returned by WScript.Shell.CreateShortcut() - provides useful information.

这篇关于使用 powershell 获取快捷方式 (.lnk) 文件的目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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