windows - 如何使用powershell同时为目录中的所有.exe文件创建多个快捷方式
问题描述
嗨,我想使用 powershel 和类似的东西同时创建几个快捷方式
Get-ChildItem -Path D:\something\ -Include *.exe -File -Recurse -ErrorAction SilentlyContinue
获取结果并为所有 .exe 文件生成快捷方式(.lnk 文件)
(.exe 只是文件类型的一个例子)
你能帮我吗?谢谢
解决方案
要在目录中创建所有文件的快捷方式.exe
,您可以执行以下操作:
- 创建Windows 脚本宿主COM 对象以创建快捷方式。您可以查看使用 MSDN 中的New-Object 创建 COM 对象以获取更多信息。
- 获取
.exe
目录中的所有文件。类似于你已经用Get-ChildItem
. - 迭代这些文件中的每一个。可以使用
foreach
或Foreach-Object
在这里。 BaseName
从文件中提取。这意味着test
从test.exe
. 我们需要它来制作快捷方式文件。- 从路径创建快捷方式。该路径只是目标路径+文件名+
.lnk
扩展名。我们可以使用Join-Path
here来制作这条路径。 - 将快捷方式的目标路径设置为可执行文件并保存快捷方式。
示范:
$sourcePath = "C:\path\to\shortcuts"
$destinationPath = "C:\path\to\destination"
# Create COM Object for creating shortcuts
$wshShell = New-Object -ComObject WScript.Shell
# Get all .exe files from source directory
$exeFiles = Get-ChildItem -Path $sourcePath -Filter *.exe -Recurse
# Go through each file
foreach ($file in $exeFiles)
{
# Get executable filename
$basename = $file.BaseName
# Create shortcut path to save to
$shortcutPath = Join-Path -Path $destinationPath -ChildPath ($basename + ".lnk")
# Create shortcut
$shortcut = $wshShell.CreateShortcut($shortcutPath)
# Set target path of shortcut to executable
$shortcut.TargetPath = $file.FullName
# Finally save the shortcut to the path
$shortcut.Save()
}
推荐阅读
- android - 为什么在 Constraintlayout 中为视图提供偏差约束会影响调整动画大小?
- android - 如何在蓝牙 POS 打印机中打印右对齐的文本?
- swagger - $ref 响应示例
- matlab - 有没有办法根据数组中的元素在matlab中屏蔽矩阵?
- gradle - 无法在数据中心框架 MarkLogic 中运行协调流
- php - 无法使用 try catch 块捕获 PHP file_get_contents 错误
- javascript - 将图像动态添加到列表元素
- protractor - 量角器中的登陆页面验证
- python - 用于 Jinja 的 Dynamic Flask-Form 结构
- java - 如何从 URL 解析简单的 JSON 数组?