PowerShell Ezine, Logon Scripts

Guy's Scripting Ezine 119 - Shortcuts

Guy's Scripting Ezine 119 - VBScript Desktop Shortcuts

 ♣

This Week's Secret

If the car was the only form of transport that you knew about, then you would miss out flying in a plane or sailing in ship.  So it is with scripting, if you only knew about network objects then you are denying your self a whole area of scripting, for example shell objects.

So that I can troubleshoot scripts, I like to increase my range of CreateObject("WScript.NOUNS").  Take the case, where I cannot get the correct method to work, for example obj.CreateShortcut or obj.SendKeys.  The underlying problem is that before I can use .createShortCut, I must create a shell object and not a network object.  Consequently, this is what I would script: CreateObject("WScript.Shell").  CreateShortcut and indeed SendKeys, are methods available for shell objects, but are not available if you create network objects.

This Week's Mission

This week's mission is to create a desktop shortcut.  You may wish to do this if you have a proprietary program that you are rolling out to a group of users.  The idea is to provide a desktop shortcut via a logon script.  What happens is users double click on the shortcut and thus execute their program, which may even be on a remote network share.


If you are looking for handy network utilities, try some of the free downloads at Tools4Ever


Example Script: Create a desktop shortcut

Pre-Requisites

None.  However, after one or two test runs you may wish to change the variables in order to experiment with other descriptions, icons, or programs.

Instructions

  1. Copy and paste the script below into notepad or get a script editor such as OnScript.
  2. Save the file with .vbs extension e.g. CreateShortcut.vbs
  3. Double click your script and check your desktop for a new icon called WordProcess.

' CreateShortCut.vbs - Create a Desktop Shortcut.
' VBScript to create .lnk file
' Author Guy Thomas http://computerperformance.co.uk
' Version 2.4 - July 2006
' ----------------------------------------------------------'
Option Explicit
Dim objShell, objDesktop, objLink
Dim strAppPath, strWorkDir, strIconPath

' --------------------------------------------------
' Here are the variables that to change if you are making a 'real' script

strWorkDir ="C:\windows"
strAppPath = "%SystemRoot%\notepad.exe"
strIconPath = "%SystemRoot%\system32\SHELL32.dll,5"

Set objShell = CreateObject("WScript.Shell")
objDesktop = objShell.SpecialFolders("Desktop")
Set objLink = objShell.CreateShortcut(objDesktop & "\WordProcess.lnk")

' ---------------------------------------------------
' Section which adds the shortcut's key properties

objLink.Description = "WordProcess"
objLink.HotKey = "CTRL+SHIFT+X"
objLink.IconLocation = strIconPath
objLink.TargetPath = strAppPath
objLink.WindowStyle = 3
objLink.WorkingDirectory = strWorkDir
objLink.Save

WScript.Quit

' End of creating a desktop shortcut

Learning Points

Note 1:  Observe how we create a shell object with Set objShell = CreateObject("WScript.Shell")

Note 2:  Once we have created the shell object then we can manipulate it with methods, for example .CreateShortCut

Note 3:  On its own, the shortcut link is not much use; we need to give it power by adding properties, the most important of which is .TargetPath.

Note 4:  Trace how the variables control the objLink properties.  Employing variables makes it easier to amend the script and thus create shortcuts which launch different applications.

Note 5:  See how the script takes care of the path with: objDesktop = objShell.SpecialFolders("Desktop")

VBScript Desktop Shortcut Challenges

1) Amend the icon. This is just a cosmetic change in the appearance of the desktop icon. Try replacing 5 with 175 in: strIconPath = "%SystemRoot%\system32\SHELL32.dll,175".  To research the available icons, right click the icon, select properties, Change Icon and make your choice.  It seems to me that the logic was the first icon is zero, then you count down (row-wise), thus the bottom of the first column is 3, top of the second column 4 and so on.

2) Research your own applications.  If you cannot think of any try WordPad, Paint or even Freecell.  Once you have found another executable, amend the strAppPath.

3) Create a Windows 8 Metro UI Tile

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers.  Download the Free IP Address Tracker

Summary of Creating Shortcuts with VBScript

In order for VBScript to create a desktop shortcut, first create a shell object.  The secret is then to add appropriate values for the application, the description and the icon.  As with so many scripts, these values are best controlled by variables.  One practical use would be to supply a shortcut via a logon script so that users had access to a new network application.

If you like this page then please share it with your friends

 


Windows Vista Registry Tweaks:

 

   Tweak the Registry ebook

Download Your Tweak the Registry Ebook for only $6.45

This ebook will explain the workings of the registry.  I thoroughly enjoy tweaking the registry, and I want to distill the best of my experiences and pass them on to you.

Each registry tweak has two aims; to solve a specific problem, and to provide general learning points, which help you to master regedit. 

Over 60 pages ebook and PDF format

 

 


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of operating system information.  Take the guess work out of which WMI counters to use for scripts.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.