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.