Srini
Srini

Reputation: 21

Create shortcut which runs in minimized mode via script

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

Upvotes: 1

Views: 3967

Answers (3)

Sourav Ghosh
Sourav Ghosh

Reputation: 2024

@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

Upvotes: 2

npocmaka
npocmaka

Reputation: 57322

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.

Upvotes: 2

Binh D.
Binh D.

Reputation: 41

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

Upvotes: 3

Related Questions