Mostly we will simply copy an already created shortcut but With PowerShell you can create a shortcut by using the New-Object cmdlet. Let we look in to this detail below. Here’s a quick script to create a shortcut to Notepad and put it on the desktop.
Step #1: The First 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.
$SourceFileLocation = “$env:SystemRoot\System32\notepad.exe”
$ShortcutLocation = “C:\Users\thiyagu.a.selvaraj\Desktop\Notepad.lnk”
Step #2: The second step is to create a variable referencing a Wscript.Shell COM Object.
$WScriptShell = New-Object -ComObject WScript.Shell
Step #3: The third step is to add the target path to the method
$Shortcut = $WScriptShell.CreateShortcut($ShortcutLocation)
$Shortcut.TargetPath = $SourceFileLocation
Step #4: The final step is to envoke the Save() method to save your shortcut.
$Shortcut.Save()
Final Code
1 2 3 4 5 6 7 8 9 10 |
# Create a Shortcut with Windows PowerShell $SourceFileLocation = "$env:SystemRoot\System32\notepad.exe" $ShortcutLocation = "C:\Users\thiyagu.a.selvaraj\Desktop\Notepad.lnk" #New-Object : Creates an instance of a Microsoft .NET Framework or COM object. #-ComObject WScript.Shell: This creates an instance of the COM object that represents the WScript.Shell for invoke CreateShortCut $WScriptShell = New-Object -ComObject WScript.Shell $Shortcut = $WScriptShell.CreateShortcut($ShortcutLocation) $Shortcut.TargetPath = $SourceFileLocation #Save the Shortcut to the TargetPath $Shortcut.Save() |
What do you think?
I hope you have an idea of how to create the Create Shortcuts on Desktops using Powershell. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.
How could you do this for multiple computers
Good day, I have a program that needs the ” ” taken out of the target path how do you remove that out of the target box. it is not just as simple as to remove them from the script
When using this method, how would we remove the shortcut for all users?
Hello there, thank you:)
Please let me know how i can add a shortcut of any app which i install from command prompt. Let say i have one ABC.exe app which i want to install and create a shortcut for the same on windows desktop.
Shall i just say,
“$env:SystemRoot\System32\ABC.exe” and run this command in powershell ?