Deploy Desktop Shortcut with Custom Icon File

Purpose:
You wish to deploy a shortcut (URL, UNC path, local path) with a custom icon (.ico) file to all users.

Resolution:
Deploy a shortcut (URL/UNC//local path) using a script and a custom icon file (.ico).

The Powershell Way

1. Create a .ps1 file called LinkMaker.ps1 (Or whatever you want.  Live your life, be creative, fly baby bird) that contains the following:

function New-Shortcut {
param (
[Parameter(Mandatory = $true)]
$ShortcutLocation,
[Parameter(Mandatory = $true)]
$ShortcutName,
[Parameter(Mandatory = $true)]
$IconPath,
[Parameter(Mandatory = $true)]
$WorkingDirectory,
$TargetPath,
$Arguments
)

$ShortCutPath = $ShortcutLocation + "\" + $ShortcutName + ".lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutPath)
$Shortcut.TargetPath = $targetPath
$shortcut.Arguments = $arguments
$shortcut.IconLocation = $iconPath
$shortcut.workingdirectory = $WorkingDirectory
$Shortcut.Save()
}

$args = @{
ShortcutLocation = "$env:PUBLIC\Desktop"
ShortcutName = "Gooble Chrome"
TargetPath = "C:\Program Files\Google\Chrome\Application\chrome.exe"
Arguments = ""
IconPath = "C:\Program Files\Google\Chrome\Application\chrome.exe"
WorkingDirectory = "C:\Program Files\Google\Chrome\Application"
}

New-Shortcut @args

NOTES:

  • The script creates a function in memory and then splats (the @args part) the arguments to the function.  If you're not familiar with splatting parameters that is fine.  All you really need to change is the values after the '=' signs in the "args" section.
  • $args = @{
    ShortcutLocation = "$env:PUBLIC\Desktop"
    ShortcutName = "Google Chrome"
    TargetPath = "C:\Program Files\Google\Chrome\Application\chrome.exe"
    Arguments = ""
    IconPath = "C:\Program Files\Google\Chrome\Application\chrome.exe"
    WorkingDirectory = "C:\Program Files\Google\Chrome\Application"
    }
  • You'll want to change the values of some of the options above when you create your shortcut.  I'll explain what each value is below.
    • ShortcutLocation: Mandatory.  This is where you want your shortcut to be placed on the computer.
    • ShortcutName: Mandatory.  The name of your shortcut
    • TargetPath: Mandatory.  The target the shortcut points at.  This can be an exe, ps1, bat file, etc.
    • Arguments: Optional.  If any arguments need to be passed to the file at the target path when the shortcut is clicked they go here.  If none are needed you can simply enter "" as above.
    • IconPath: Mandatory.  The path to the icon for the shortcut.  In most cases if you put in the path to the executable it will use the same icon.  You can also enter the path to a .ico file.
    • WorkingDirectory: Optional.  The directory to start in when opening the application.  It just makes the app open a little faster.

2. Save the file either to your PDQ Repository or another share.

Create a package in PDQ Deploy

3. Create a New Package in PDQ Deploy. Name the package something meaningful.

4. Add a Powershell step and either click "Insert Powershell Script".  Using this method you can make your changes in the script and save it before deploying.  The deployment will grab the script file you point it at each time it runs.

 

InsertPowershellScript.png

OR

Copy and paste the script into your PowerShell step.  Using this method you can edit the script in the PowerShell windows which allows you to change it between deployments inside of the package.

CopyScript.png

5. If you're going to be using a .ico file for the icon create a file copy step to copy the icon file to the target machine.

FileCopy.png

6. Save and test the deployment.

The Batch File Way

1. Create a .bat file called LinkMaker.bat that contains the following:

@echo off

set SCRIPT="%TEMP%\LinkMaker-%RANDOM%-%RANDOM%.vbs"

echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo sLinkFile = "%PUBLIC%\Desktop\MyLink.lnk" >> %SCRIPT%
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
echo oLink.TargetPath = "http://example.com" >> %SCRIPT%
echo oLink.IconLocation = "%PUBLIC%\documents\Example.ico" >> %SCRIPT%
: echo oLink.Arguments = ""  >> %SCRIPT%
: echo oLink.Description = "" >> %SCRIPT%
: echo oLink.HotKey = ""  >> %SCRIPT% 
: echo oLink.Arguments = ""  >> %SCRIPT%
: echo oLink.WindowStyle = ""  >> %SCRIPT%
: echo oLink.WorkingDirectory = ""  >> %SCRIPT%
echo oLink.Save >> %SCRIPT%

cscript /nologo %SCRIPT%
del %SCRIPT%

NOTES:

  • This creates a temporary vbs file in Windows\Temp with a random name. The vbs file is deleted at the end of the batch script.
  • The reason you want the files in %PUBLIC% is so that all users receive the link and the link can only be removed by a member of the Administrators group.
  • Uncomment (::) and add the appropriate information if desired.
  • Change the sLinkFile to the name of your .lnk file (keep the path).
  • Change the the oLink.TargetPath to the URL/UNC/local path.
  • Change the oLink.IconLocation to the path of the the .ico file on the target machine(s).

2. Save the file either locally or on a UNC share.

Create a package in PDQ Deploy

1. Create a New Package in PDQ Deploy. Name the package something meaningful.

2. Create a Copy step in that package (PDQ Deploy Enterprise mode required).

The source is going to be your icon file on the PDQ console machine and the target folder is going to be %public%\documents\ (this is the oLink.IconLocation in the script).

3. Create an Install step. Call the bat file from above.

4. Save and test the deployment.

EDIT March 30, 2017
One of our customer, Mike (thanks Mike!), wanted to be able to use Chrome to open the URL. This is what he and his 

@echo off
echo Set oWS = WScript.CreateObject("WScript.Shell") > CreateShortcut.vbs
echo sLinkFile = "%PUBLIC%\Desktop\Whatever.lnk" >> CreateShortcut.vbs
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
echo oLink.TargetPath = "C:\Program Files\Google\Chrome\Application\chrome.exe" >> CreateShortcut.vbs
echo oLink.Arguments = "https://www.google.com/" >> CreateShortcut.vbs
echo oLink.IconLocation = "%PUBLIC%\documents\Whatever.ico" >> CreateShortcut.vbs
echo oLink.WindowStyle = "3" >> CreateShortcut.vbs
echo oLink.Save >> CreateShortcut.vbs

cscript /nologo CreateShortcut.vbs
del CreateShortcut.vbs

This has not been tested in our environment, so please test before deploying.

See Also:
Shortcut.exe Explained
How to run .reg, .bat .vbs and .ps1 files in PDQ Deploy
Copying and Deleting Shortcuts in PDQ Deploy

Still have a question or want to share what you have learned? Visit our Community to get help and collaborate with others.