Loading

Monday, December 7, 2009

Create Windows Shortcuts With a Script at the Command Line

If you are like me you 1) access numerous Windows computers, in my case mainly servers through Remote Desktop; and 2) like to have your Windows desktop setup a certain way.  Here are some examples of a quick and easy way to create shortcuts with a script.  These examples are Visual Basic Scripts (.vbs) and use Windows Script Host to execute.  Create any or all of the examples and execute it from either the command prompt or Start / Run using:
wscript xyz.vbs
Note: These scripts were all tested on Windows 7, Windows 2008 and 2003.  They should run fine on earlier versions of Windows (XP, Vista, 2000, etc.) as well.

Although most of these examples will create shortcuts to Windows Explorer (the last one is a shortcut to the Command Prompt), they are being placed in different locations.  Of course you could modify the examples to launch any program of your choosing.  Additionally you could combine them into one script that could be launched the first time you logon.

For easy reference I highlighted the values you may want to change to tailor the script to your needs.

Windows 7, Vista and Windows 2008 Server note: You will probably have to execute these with administrative rights.  One way to do this is to launch a command prompt (the old fashioned way - Start [All] Programs / Accessories / Command Prompt) using right-click and selecting "Run As Administrator."






Example 1 - Shortcut to Windows Explorer in the "All Users" Desktop folder.  I named the script Explorer_Shortcut_on_AU_Desktop.vbs.
set WshShell = WScript.CreateObject("WScript.Shell" )
strDesktop = WshShell.SpecialFolders("AllUsersDesktop" )
set oShellLink = WshShell.CreateShortcut(strDesktop & "\Windows Explorer.lnk" )
oShellLink.TargetPath = "%SYSTEMROOT%\explorer.exe"
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "%SystemRoot%\explorer.exe"
oShellLink.Description = "Windows Explorer"
oShellLink.WorkingDirectory = "%HOMEPATH%"
oShellLink.Save
Example 2 - Shortcut to Windows Explorer in the "All Users" Start Menu folder.  I named the script Explorer_Shortcut_in_AU_Startmenu.vbs.
set WshShell = WScript.CreateObject("WScript.Shell" )
strStartMenu = WshShell.SpecialFolders("AllUsersStartmenu" )
set oShellLink = WshShell.CreateShortcut(strStartMenu & "\Windows Explorer.lnk" )
oShellLink.TargetPath = "%SYSTEMROOT%\explorer.exe"
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "%SystemRoot%\explorer.exe"
oShellLink.Description = "Windows Explorer"
oShellLink.WorkingDirectory = "%HOMEPATH%"
oShellLink.Save
Example 3 - Shortcut to Windows Explorer in the "All Users" Startup folder.  I named the script Explorer_Shortcut_in_AU_Startup.vbs.  This will cause one instance of Windows Explorer to launch during logon.  If you're like me you will be using it anyway, so why not have it open automatically.
set WshShell = WScript.CreateObject("WScript.Shell" )
strStartup = WshShell.SpecialFolders("AllUsersStartmenu" )
set oShellLink = WshShell.CreateShortcut(strStartup & "\programs\startup\Windows Explorer.lnk" )
oShellLink.TargetPath = "%SYSTEMROOT%\explorer.exe"
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "%SystemRoot%\explorer.exe"
oShellLink.Description = "Windows Explorer"
oShellLink.WorkingDirectory = "%HOMEPATH%"
oShellLink.Save
Example 4 - Shortcut to Windows Explorer in the "Current User" Quick Launch toolbar.  I named the script Explorer_Shortcut_in_CU_QuickLaunch.vbs.
set WshShell = WScript.CreateObject("WScript.Shell" )
strStartup = WshShell.SpecialFolders("AppData" )
set oShellLink = WshShell.CreateShortcut(strStartup & "\Microsoft\Internet Explorer\Quick Launch\Windows Explorer.lnk" )
oShellLink.TargetPath = "%SYSTEMROOT%\explorer.exe"
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "%SystemRoot%\explorer.exe"
oShellLink.Description = "Windows Explorer"
oShellLink.WorkingDirectory = "%HOMEPATH%"
oShellLink.Save
Example 5 - Shortcut to Command Prompt in the Quick Launch toolbar for you, the current user.  I named the script CMD_Shortcut_in_CU_QuickLaunch.vbs.
set WshShell = WScript.CreateObject("WScript.Shell" )
strStartup = WshShell.SpecialFolders("AppData" )
set oShellLink = WshShell.CreateShortcut(strStartup & "\Microsoft\Internet Explorer\Quick Launch\Command Prompt.lnk" )
oShellLink.TargetPath = "%SYSTEMROOT%\system32\cmd.exe"
oShellLink.WindowStyle = 1
oShellLink.Hotkey = "Ctrl+Alt+C"
oShellLink.IconLocation = "%SystemRoot%\system32\cmd.exe"
oShellLink.Description = "Windows Command Prompt"
oShellLink.WorkingDirectory = "%HOMEPATH%"
oShellLink.Save
See also:

2 comments:

  1. Hi, it's great article. Can you advice me how to call command (TargetPath) like

    cmd /C start http://localhost/page.html

    ?

    If I run the script, It create a shortcut with target like

    "c:\cmd \C start http:\\localhost\page.html"

    Thank you, BR Jan.

    ReplyDelete
  2. strStartup is not working it says command not recognized

    ReplyDelete