www.infralib.com

www.infralib.com
Please visit my new site http://www.infralib.com for my and other authors' newer posts, articles. We will also have forums, videos, webcasts and etc.

Friday, 11 November 2011

Create Desktop Shortcut with Script

Hi all

Today, I have received an application to deploy with SMS Server 2003, I had to create a MSI Package and some scripts to make that application installation most user friendly, actualy without need to user interaction. So I have needed a script that can find logged user profile folder and create a desktop shortcut to the configuration file in user profile folder.

I have found an article about how to create desktop shortcuts at Technet http://www.microsoft.com/technet/scriptcenter/guide/sas_wsh_aytf.mspx?mfr=true , it is detailed enough to understand how to do it,

Technet Script :

Set objShell = WScript.CreateObject("WScript.Shell")
strDesktopFolder = objShell.SpecialFolders("AllUsersDesktop")
Set objShortCut = objShell.CreateShortcut(strDesktopFolder & "\IIS Manager.lnk")
objShortCut.TargetPath = "%SystemRoot%\System32\Inetsrv\iis.msc"
objShortCut.Save


As you see above script creates WshShortcut object by calling CreateOBject, than it defines Desktop folder path with strDesktopFolder by AllUsersDesktop value. But we will change this script to locate and crete a desktop shortcut of a file that unique for every user.

First of all we need to find logged users profile path, to do this we will get USERSPROFILE value from Windows Environment Settings by adding this line

UsersProfile = objShell.ExpandEnvironmentStrings("%USERPROFILE%")

now we know logged users profile path has defined in UsersProfile (e.g. C:\Documents and Settings\OShener)

*Tip : You can get another Windows Environment variables values by using objShell.ExpandEnvironmentStrings by changing variable name in (%%) , you can check your existing environment variables by running set command in command promt too.

So where is our user specific target file for shortcut ? Let assume it is located in DW folder and the DW folder is in the the logged users profile folder. Now we are going to define a variable to fix the target file path with this line:

objShortCut.TargetPath = UsersProfile & "\DW\DW.conf"

and to find and define logged users desktop path to a variable we add this line to the script too

strDesktopFolder = objShell.SpecialFolders("Desktop")

and we have to define the Desktop Shortcut location before create it

Set objShortCut = objShell.CreateShortcut(strDesktopFolder & "\DWConf.lnk")


Ok, let tidy up the script and see what we have now ?

Set objShell = WScript.CreateObject("WScript.Shell")
UsersProfile = objShell.ExpandEnvironmentStrings("%USERPROFILE%")
strDesktopFolder = objShell.SpecialFolders("Desktop")
Set objShortCut = objShell.CreateShortcut(strDesktopFolder & "\DWConf.lnk")
objShortCut.TargetPath = UsersProfile & "\DW\DW.conf"
objShortCut.Save


If you wish you can add different properties to your shortcut too, like Windows HotKey, Description, Working Folder, only thing you need to do that add ObjShortcut properties as : objShortCut.HotKey = "Ctrl+Shift+D" and refer to the link : http://www.microsoft.com/technet/scriptcenter/guide/sas_wsh_aytf.mspx?mfr=true

Regards

Osman Shener 19th-09-2007

No comments: