创建运行批处理文件的快捷方式 [英] Create shortcut that runs a batch file

查看:108
本文介绍了创建运行批处理文件的快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Powershell脚本,该脚本在Windows 7任务栏中创建一个快捷方式,该快捷方式从cmd.exe运行批处理文件.

I want to create a powershell script that creates a shortcut in the windows 7 taskbar, that runs a batch file from cmd.exe.

尝试按照这两篇文章中的说明进行操作:

Trying to do as told in these two posts:

  1. 如何使用Powershell创建快捷方式
  1. https://superuser.com/questions/100249/how-to-pin-either-a-shortcut-or-a-batch-file-to-the-new-windows-7-taskbar
  2. How to create a shortcut using Powershell

基本上我想将快捷方式文件Target属性设置为类似

Basically I want to set a shortcut files Target property to be something like:

C:\Windows\System32\cmd.exe /C "C:\Dev\Batch files\cmake-guiMSVC1064bit.bat"

到目前为止,我在Powershell脚本中得到的是:

What I got so far in my powershell script is:

$batchPath = "C:\Dev\my_batchfile.bat"
$taskbarFolder = "$Home\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\Taskbar\"
$cmdPath = (Get-Command cmd | Select-Object Definition).Definition
$objShell = New-Object -ComObject WScript.Shell
$objShortCut = $objShell.CreateShortcut("$shortcutFolder\$batchName.lnk")

#TODO problem ... :(
$objShortCut.TargetPath = "$cmdPath /C $batchPath"

$objShortCut.Save()

这将导致以下错误:

Exception setting "TargetPath": "The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))" At C:\Dev\Powershell\GetTools.ps1:220 char:18
+     $objShortCut. <<<< TargetPath = "$cmdPath /C $batchPath"
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

有人有任何建议吗?

推荐答案

通过Arguments属性设置参数:

Set the arguments via the Arguments property:

$batchName = 'cmd'
$batchPath="D:\Temp\New folder\test.bat"
$taskbarFolder = "$env:APPDATA\Microsoft\Internet Explorer\Quick Launch\User Pinned\Taskbar\"
$objShell = New-Object -ComObject WScript.Shell
$objShortCut = $objShell.CreateShortcut("$taskbarFolder\$batchName.lnk")
$objShortCut.TargetPath = 'cmd'
$objShortCut.Arguments="/c ""$batchPath"""
$objShortCut.Save()

这篇关于创建运行批处理文件的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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