1

How do I create a shortcut of a batch file and configure if to run in minimized mode? When I create a shortcut to a batch file I have to manually configure it to run in minimized mode manually. Any idea how do I write a script to change it to run as "minimized" mode

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
Srini
  • 21
  • 1
  • 5
  • 1
    This may help you: https://stackoverflow.com/questions/9232308/how-do-i-minimize-the-command-prompt-from-my-bat-file – Sourav Ghosh May 04 '17 at 16:28
  • I checked that but my requirement is to start my batch file in minimized mode by not having to : 1. Right-click on your shortcut 2. Select Properties. 3. Under Run, you can select Minimized, Maximized, or Normal Window. 4. Click OK. – Srini May 04 '17 at 16:31
  • IMHO, this is not possible using batch scripting. You have to change it manually in the shortcut. Or use any other methods from the other question. Those will just show the window for 1 second probably and then will run minimized. – Sourav Ghosh May 04 '17 at 16:38
  • 1
    Do you want to avoid the manual adjustment that needs to be done _just once_? The shortcut must be created manually just once also... – Aacini May 04 '17 at 17:55

3 Answers3

2

If I understand you correctly. You will need to use VB script to create shortcut. I don't believe batch script can create shortcut

https://support.microsoft.com/en-us/help/244677/how-to-create-a-desktop-shortcut-with-the-windows-script-host

see example 2: the WindowsStyle parameter define the windows size. oMyShortCut.WindowStyle = 7 <-- 7= minimized.

Good luck Binh

Binh D.
  • 31
  • 2
2

@npocmaka's shortcutjs.bat is a complete solution but it has about 200 lines. So, I have created a small VBScript for the purpose. You need to modify it according to your purpose.

'======PART 1: elivate to admin. required to save the batch file from part 2 in C drive
If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , WScript.ScriptFullName & " /elevate", "", "runas", 1
  WScript.Quit
End If

'======PART 2: create the test batch file on the fly
Set objFSO=CreateObject("Scripting.FileSystemObject")
outFile = "c:\test.cmd"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write "pause" & vbCrLf
objFile.Close

'=======PART 3: create the shortcut of the batch file
set WshShell = CreateObject("Wscript.shell")
strDesktop = WshShell.SpecialFolders("Desktop")
set oMyShortcut = WshShell.CreateShortcut(strDesktop + "\test.lnk")
oMyShortcut.WindowStyle = 7
OMyShortcut.TargetPath = "c:\test.cmd"
oMyShortCut.Save

Part 1 and 2 are optional and they are just to give an idea about what to do if you also want to create the batch file on the fly. Part 3 is the required code to create a shortcut using VBS.

You can run VBS script from cmd: cscript shortcut.vbs after you save the code above as shortcut.vbs

If you want to pass some argument about your batch file location, see this question, Can I pass an argument to a VBScript (vbs file launched with cscript)? Then you can also use your code like cscript shortcut.vbs "C:\test.cmd" and reuse the same VBScript to create different shortcuts.

For other available options like adding an icon to your shortcut, adding hotkey support, setting Working Directory etc. please see this link

Community
  • 1
  • 1
Sourav Ghosh
  • 1,964
  • 4
  • 33
  • 43
  • 1
    I ended up using part3 of your script and integrated it with my automation script. Thank you! This solution works better for me – Srini May 05 '17 at 06:13
1

Try with shortcutjs.bat:

shortcutjs.bat -linkfile tst6.lnk -target "%cd%\myscript.bat" -windowstyle 7 -adminpermissions yes

-adminpermissions yes is optional if you want to run the bat as administrator. You'll need the full path to your script. possible modes are 1 for normal, 3 for maximized and 7 for minimized.

npocmaka
  • 55,367
  • 18
  • 148
  • 187