Hey, Scripting Guy! I love the Quick Launch toolbar. The problem is that I cannot find a special folder or anything that refers to this location. So how can I create a shortcut to Windows PowerShell that will live on the Quick Launch toolbar?
One of the funny things about special folders is that they, for the most part, really are not all that special. They are just folders after all, but they live in a special place. On the other hand, as we saw in the last "Hey, Scripting Guy!", special folders have coded values that can make referring to them easier. When they are missing it is a major bummer. So maybe they are special after all. RS, as you no doubt discovered, the Quick Launch toolbar is not a special folder at all. Don't know why, but it just did not quite make the cut to be special. So I guess it is only semispecial. With semispecial folders, we have to work with the path ourselves. Luckily, in Windows PowerShell we have a cmdlet that makes it rather easy to do. See also a VBScript version of this script.
Here is the CreateShortCutOnQuickLaunch.ps1 script:
$appName = "PowerShell" $arguments = " -noProfile -nologo" $wshshell = new-object -comobject wscript.shell $volEnv = $wshShell.environment("volatile") $qlf = join-path -path $volEnv.item("appdata") -childPath "Microsoft\Internet Explorer\Quick Launch" $shortCut = $wshShell.CreateShortCut("$qlf\$appName.lnk") $shortCut.TargetPath = $appName $shortCut.Description = "Lanch $appName" $shortCut.WorkingDirectory = "c:\" $shortCut.HotKey = "CTRL+SHIFT+P" $shortCut.Arguments = $arguments $shortCut.Save()
When we run the CreateShortCutOnQuickLaunch.ps1 script, we are greeted with the friendly Windows PowerShell icon on our Quick Launch toolbar:
In the first line of the script, we assign the string PowerShell to the $appName variable. This is seen here:
$appName = "PowerShell"
We next use the variable $arguments to hold the arguments we will pass to the application when it is launched via the shortcut. For Windows PowerShell, we will be starting Windows PowerShell without using a profile and without displaying the logo. These arguments are seen here:
$arguments = " -noProfile -nologo"
As seen in the image following this paragraph, the top Windows PowerShell window is launched with no arguments. The top two lines are controlled by the -nologo parameter. The window below was created with the -noprofile and the -nologo parameter. If it seems hardly worth the trouble, I might very well be inclined to agree with you—but think how bad it would be if you did not have this information. We scripting guys are always prepared to go the extra mile for our readers, so we added it to the script:
Now we will create an instance of the Wscript.Shell object. The Wscript.Shell object is documented here. Even though the documentation is focused on VBScript, the methods and properties work in a similar fashion for Windows PowerShell. The line of code that creates the WshShell object is seen here:
$wshshell = New-Object -comobject wscript.shell
We can use the environment property to retrieve the path to our volatile environment settings. This is returned as a collection of the items that make up the volatile environment. Here are the volatile environment settings on my computer:
PS C:\> $volEnv LOGONSERVER=\\OFFICE CLIENTNAME=Console SESSIONNAME=Console APPDATA=C:\Documents and Settings\ed\Application Data HOMEDRIVE=C: HOMESHARE= HOMEPATH=\Documents and Settings\ed
To create the collection of the volatile environment, we use this line of code:
$volEnv = $wshShell.environment("volatile")
Now we need to create the path to the Quick Launch toolbar. To work with paths in Windows PowerShell, we can use the Join-Path cmdlet. We give the -path parameter the path to the appdata folder, which we can obtain from the volatile environment. When we have done this, we specify the -childpath parameter. We use a string to represent the location for the Quick Launch folder—it is in Microsoft\Internet Explorer\Quick Launch. When we put these two pieces together, we have the path to the Quick Launch folder. We store the resulting path object in the $qlf variable. This line of code is seen here:
$qlf = join-path -path $volEnv.item("appdata") -childPath "Microsoft\Internet Explorer\Quick Launch"
Now we simply need to create a shortcut. To do this, we use the same methodology we would have used in VBScript. We first need to use the CreateShortCut method. This method from the WshShell object needs to be given the path to create the shortcut, as well as the name of the shortcut followed by the .lnk extension. This is seen here:
$shortCut = $wshShell.CreateShortCut("$qlf\$appName.lnk")
The cool thing about Windows PowerShell is that we can use an expanding string to create our path for the CreateShortCut method. An expanding string is in double quotation marks " ", and a literal string is in single quotation marks ' '. The advantage of an expanding string is that the value of a variable is expanded when it is printed. As seen in the image following this paragraph, when the variable is placed in double quotation marks, it expands to display the value. When it is placed in single quotation marks, the variable name is displayed. If the variable is escaped with the back tick character (`), it is not expanded:
The targetpath property is used to tell the shortcut which application to launch. Because Windows PowerShell is in a well-known directory, we do not need to specify the full path to the location of the executable; therefore, in the $appName, we only gave it the name PowerShell. If you were going to modify this script to create a shortcut for some other application, you would need to test whether or not the script could find the executable without the full path:
$shortCut.TargetPath = $appName
The description can be anything, but I generally prefer to use the application name somewhere so that I do not need to rely upon recognizing the icon. Once again we use an expanding string to avoid having to concatenate the word “Launch” and the name of the application. This is seen here:
$shortCut.Description = "Lanch $appName"
Now we need to add the working directory. This is one of the main reasons I like creating a shortcut to Windows PowerShell. I prefer to have my working directory open on the root of the drive because it gives me more room to type. By default, Windows PowerShell opens in your profile location, which ensures you have rights:
$shortCut.WorkingDirectory = "c:\"
We now set the keyboard shortcut for the shortcut by specifying the combination of keystrokes we wish to use:
$shortCut.HotKey = "CTRL+SHIFT+P"
The last property we populate is the arguments section of the script. We talked about arguments when we assigned them to the variable. These are what you wish to pass into the executable as command-line arguments. These customarily will change the way an application behaves when it is run. Here is the line of code that does this:
$shortCut.Arguments = $arguments
The last thing we need to do is to save the shortcut. This is as simple as saying save:
When our script is done, we can view the properties of the shortcut by right-clicking the shortcut and clicking Properties. This is seen here:
Well, RS, that is about all there is to creating a shortcut on the Quick Launch toolbar. As you can see, even if it is not a special folder, it is still pretty special. Happy to help.
Ed Wilson and Craig Liebendorfer, Scripting Guys