4

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. 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

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"

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

This results in the following error:

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

Anyone got any suggestions?

5
  • I already have the batch file, just need to make a powershell script that creates a shortcut where the target property is something like: C:\Windows\System32\cmd.exe /C "C:\Dev\Batch files\cmake-guiMSVC1064bit.bat" Commented Jun 19, 2012 at 7:34
  • Why not right click on the batch file->create shortcut...? Commented Jun 19, 2012 at 7:39
  • Because this is part of a larger script that is supposed to be run on multiple machines with a minimum of human interaction. :) Commented Jun 19, 2012 at 7:44
  • Ah, okay. Anyway, where is $batchPath defined? Maybe that's your problem. Commented Jun 19, 2012 at 7:47
  • Just nissed that variable in the copy/paste, fixed it in the post now, still got the same problem. Commented Jun 19, 2012 at 7:55

1 Answer 1

10

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.