Create Shortcuts .lnk or .url Files With PowerShell
Have you ever needed to create shortcuts while scripting. Shortcuts are simply .lnk files with a few details highlighting a few details of the file that you would like to launch. Many people simply copy an already created shortcut. With PowerShell you can actually create a shortcut from scratch by utilizing the New-Object commandlet. Here is the quick run down with explanation.
Step #1: The first step is to create a variable referencing a Wscript.Shell COM Object.
$Shell = New-Object -ComObject ("WScript.Shell")
Step #2: The second step is to define the location and name of your shortcut. The following example will add the shortcut to the user’s desktop with a name of Your Shortcut.
$Shell = New-Object -ComObject ("WScript.Shell") $ShortCut = $Shell.CreateShortcut($env:USERPROFILE + "\Desktop\Your Shortcut.lnk")
Step #3: The third step is to add the target path, any relevant arguments, along with anything else that may be required.
$Shell = New-Object -ComObject ("WScript.Shell") $ShortCut = $Shell.CreateShortcut($env:USERPROFILE + "\Desktop\Your Shortcut.lnk") $ShortCut.TargetPath="yourexecutable.exe" $ShortCut.Arguments="-arguementsifrequired" $ShortCut.WorkingDirectory = "c:\your\executable\folder\path"; $ShortCut.WindowStyle = 1; $ShortCut.Hotkey = "CTRL+SHIFT+F"; $ShortCut.IconLocation = "yourexecutable.exe, 0"; $ShortCut.Description = "Your Custom Shortcut Description";
Step #4: The final step is to envoke the Save() method to save your shortcut.
$Shell = New-Object -ComObject ("WScript.Shell") $ShortCut = $Shell.CreateShortcut($env:USERPROFILE + "\Desktop\Your Shortcut.lnk") $ShortCut.TargetPath="yourexecutable.exe" $ShortCut.Arguments="-arguementsifrequired" $ShortCut.WorkingDirectory = "c:\your\executable\folder\path"; $ShortCut.WindowStyle = 1; $ShortCut.Hotkey = "CTRL+SHIFT+F"; $ShortCut.IconLocation = "yourexecutable.exe, 0"; $ShortCut.Description = "Your Custom Shortcut Description"; $ShortCut.Save()
Step #5: As a bonus here is how you would create a Favorite in Windows which is a .url shortcut.
$Shell = New-Object -ComObject ("WScript.Shell") $Favorite = $Shell.CreateShortcut($env:USERPROFILE + "\Desktop\Your Shortcut.url") $Favorite.TargetPath = "http://www.yoururl.com"; $Favorite.Save()
Hello i was wondering if it is possible and how one whould command the specified url to be opened in a non default browser.
Since sertain users need specific websites to open in specific browsers.
The noobs thing the website is a program and refuze to use favorites.
To do that you would create a shortcut to “chrome.exe” or “firefox.exe” and the argument you would pass would be the http://websitelink.com etc.
Hi i am getting below error
Error: Unable to save shortcut “\\sites\global\npss\Dev MC Order Boards and Shelf Tags\Grocery\SP\PDFs\Store PDFs\ARR_Arroyo\VINEGAR_08FT_260A300B.lnk”.
i am able to create shortcut in my local but i am not able to create on sharepoint server . could you please tell me the problem . and i need solution for this.
It looks like a permissions error trying to save it to the UNC path. Try doing a simple copy of the file to the location from PowerShell before proceeding to do the creation of a new link.
Hello i was wondering how i could push the URL Script to all desktops within my OU?
What is your goal? You can use a login script and then it will automatically create URLs on the desktop if desired. I would add logic to only copy it if it doesn’t exist.
Is there a way to incorporate this into a bat file, I have a bat file setup to do other things but I need a shortcut made as well, and would like to have this setup in 1 file rather than referencing numerous files to be ran.
You can call it via cscript.exe \\path\to\file.vbs