创建运行批处理文件的快捷方式

时间:2012-06-19 07:29:11

标签: powershell batch-file

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

试着按照这两篇文章中的说法去做:

  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
  3. 基本上我想将快捷方式文件Target属性设置为:

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

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

    $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
    

    有人有任何建议吗?

1 个答案:

答案 0 :(得分:9)

通过Arguments属性设置参数:

$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()