Converting the Windows Script Host Save Method

Definition: Saves a shortcut object to disk.

Save

At the risk of repeating ourselves, let’s repeat, word-for-word, what we said about the CreateShortcut method:

Windows PowerShell includes no built-in mechanism for creating a shortcut file; as far as we know, neither does the .Net Framework. A problem? No, not really. After all, when in doubt, you can always use Windows Script Host to create the shortcut for you:

$strDesktopFolder = [System.Environment]::GetFolderPath("Desktop")
$objShell = New-Object -com "Wscript.Shell"
$objShortcut = $objShell.CreateShortcut($strDesktopFolder + "\PowerShell.lnk")
$objShortcut.TargetPath = "C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.Exe"
$objShortcut.Save()

In our first line of code, we’re using the .Net Framework class System.Environment and the GetFolderPath method to return the path to the current user’s desktop folder. Why did we do that? Because we want to create our shortcut on the desktop. What if you don’t want to create the shortcut on the desktop? That’s fine; you can pass GetFolderPath any of the following values:

  • ApplicationData

  • CommonApplicationData

  • CommonProgramFiles

  • Cookies

  • Desktop

  • DesktopDirectory

  • Favorites

  • History

  • InternetCache

  • LocalApplicationData

  • MyComputer

  • MyDocuments

  • MyMusic

  • MyPictures

  • Personal

  • ProgramFiles

  • Programs

  • Recent

  • SendTo

  • StartMenu

  • Startup

  • System

  • Templates

After we retrieve the path to the Desktop folder we use the New-Object cmdlet (and the –com parameter) to create an instance of the Wscript.Shell object (the Windows Script Host Shell object). As soon as we have that object in hand we can then use the CreateShortcut method to create a new shortcut in memory:

$objShortcut = $objShell.CreateShortcut($strDesktopFolder + "\" + "PowerShell.lnk")

Notice that we only had to pass CreateShortcut a single parameter: the complete path to the shortcut file we want to create. In this case, our path is built up of three pieces:

  • $strDesktopFolder, the path to the user’s Desktop folder.

  • \, the standard Windows path separator.

  • PowerShell.lnk, the file name for our shortcut file.

As we noted, CreateShortcut only creates a virtual shortcut file; to turn this into a real shortcut we need to do at least two more things. First, we set the TargetPath property to the path of the executable file (or the folder, or the document, or …) that our shortcut points to. Because we’re creating a shortcut to PowerShell.exe that path (on our computer, at least) looks like this:

C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.Exe

Incidentally, you only have to specify the complete path if the file in question cannot be found anywhere in your Windows path; if the file can be found in the path then you only need to specify the file name when configuring the TargetPath:

$objShortcut.TargetPath = "PowerShell.Exe"

What if you aren’t sure if the PowerShell folder is in your Windows path? No problem; just use this command to view what is – and isn’t – in the Windows path:

Get-ChildItem Env:\Path | Format-List

That will return data similar to this:

Name  : Path
Value : C:\Perl\site\bin;C:\Perl\bin;D:\Perl\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;
C:\Program Files\ATI Technologies\ATI Control Panel;C:\PROGRA~1\CA\SHARED~1\SCANEN~1;C:\PROGRA~1\CA\ETRUST~1;C:\Program Files\HPQ\IAM\bin;
C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Scripts;C:\Program Files\Common Files\Adobe\AGL;
C:\WINDOWS\system32\WindowsPowerShell\v1.0\;C:\Program Files\QuickTime\QTSystem\

Finally, to turn our virtual shortcut into a real shortcut we use the same process Gepetto used to turn Pinocchio into a real boy: we simply call the Save method. You know, like this:

$objShortcut.Save()

See conversions of other Windows Script Host methods and properties.
Return to the VBScript to Windows PowerShell home page