I created this script for use with SCCM to allow easy creation of shortcuts during deployment for applications that do not create their own shortcut, or for portable applications that are not actually installed. The powershell script can be called upon from the “Run Command Line” step of a task sequence, and the script should be added as a Package to SCCM so that it can be referenced from the Package option inside the Run Command Line step.
This script generates shortcuts in the Public Desktop and the Start Menu. If a drive letter is specified in the path, it will be used. If no drive letter is specified in the path, the $env:SystemDrive variable will be used to figure out the drive letter based on the OS drive. If a URL is specified in the path, it will be used instead of a local path.
Here are some usage examples:
1 2 3 |
powershell -executionpolicy bypass -file generateShortcut.ps1 -name "Shortcut Name" -path "D:\test.txt" powershell -executionpolicy bypass -file generateShortcut.ps1 "Shortcut 2" "Users\Administrator\Desktop\test.txt" powershell -executionpolicy bypass -file generateShortcut.ps1 -name "Shortcut to Google" -path "https://www.google.com/" |
Here is the full script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
<# This script generates shortcuts in the Public Desktop and the Start Menu. If a drive letter is specified in the path, it will be used. If no drive letter is specified in the path, the $env:SystemDrive variable will be used to figure out the drive letter based on the OS drive. If a URL is specified in the path, it will be used instead of a local path. Usage: powershell -executionpolicy bypass -file generateShortcut.ps1 -name "Shortcut Name" -path "D:\test.txt" powershell -executionpolicy bypass -file generateShortcut.ps1 "Shortcut 2" "Users\Administrator\Desktop\test.txt" powershell -executionpolicy bypass -file generateShortcut.ps1 -name "Shortcut to Google" -path "https://www.google.com/" Script created by Boris Kagan. https://www.boriskagan.net #> # read variables passed from the command line when launching this script param($name, $path) # if inputs are strings, create the shortcut if ([bool]($name -as [string]) -and [bool]($path -as [string])) { # if the input has a drive letter, split it from the string $pathFormatted = $path -split ":" # if there is 1 character before the colon, it must be the drive letter, so set the targetPath to the original path input # if the characters before the colon are ftp, http, or https, then also use the original path input to create a web link if ($pathFormatted[0].Length -eq 1 -OR $pathFormatted[0] -eq "ftp" -OR $pathFormatted[0] -eq "http" -OR $pathFormatted[0] -eq "https") { $targetPath = "$path" } else { # else use the variable for system drive letter and add back the original path input since it doesn't include a drive letter $targetPath = "$env:SystemDrive\$path" } Write-Host "Creating shortcut: " -ForegroundColor Yellow -NoNewline Write-Host "$name" Write-Host "Shortcut path: " -ForegroundColor Yellow -NoNewline Write-Host "$targetPath" # create shell comObject to allow shortcut creation $sh = New-Object -comObject WScript.Shell # create the desktop shortcut $Desk = $sh.CreateShortcut("$env:PUBLIC\Desktop\$name.lnk") $Desk.TargetPath = "$targetPath" $Desk.Save() # create the start menu shortcut $Start = $sh.CreateShortcut("$env:ProgramData\Microsoft\Windows\Start Menu\Programs\$name.lnk") $Start.TargetPath = "$targetPath" $Start.Save() # if inputs are not strings or are empty, fail and exit } else { Write-Host "Improper input detected, shortcut will not be created." -ForegroundColor red Write-Host "This script generates shortcuts in the Public Desktop and the Start Menu. If a drive letter is specified in the path, it will be used." -ForegroundColor yellow Write-Host "If no drive letter is specified in the path, the `$env:SystemDrive variable will be used to figure out the drive letter based on the OS drive." -ForegroundColor yellow Write-Host "If a URL is specified in the path, it will be used instead of a local path." -ForegroundColor yellow Write-Host "Usage Example: " -nonewline -ForegroundColor white Write-Host 'powershell -executionpolicy bypass -file generateShortcut.ps1 -name "Shortcut Name" -path "D:\test.txt"' -ForegroundColor green Write-Host "Usage Example: " -nonewline -ForegroundColor white Write-Host 'powershell -executionpolicy bypass -file generateShortcut.ps1 "Shortcut 2" "Users\Administrator\Desktop\test.txt"' -ForegroundColor green Write-Host "Usage Example: " -nonewline -ForegroundColor white Write-Host 'powershell -executionpolicy bypass -file generateShortcut.ps1 -name "Shortcut to Google" -path "https://www.google.com/"' -ForegroundColor green exit 1 } |
Of course if you choose to save this script with a name other than generateShortcut.ps1, the usage examples will no longer be accurate.
I have tested this script on both Windows 7 and Windows 10. Let me know your thoughts, suggestions, or issues in the comments below.