Adding a Shortcut link icon to a dekstop shortcut

Hey guys this is literally the first time I’ve asked a question with regard to powershell. I’ve already searched it but none of the solutions I’ve found seem to work.

My problem is i’ve created a folder and copied all the required files into it. I can create a desktop shortcut but i can’t for the life of me figure out how to add an icon to the shortcut. I have an .ico file in the folder.

Here’s what i have so far:

$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut(“$env:USERPROFILE\Desktop\myproject.lnk”)
$Shortcut.TargetPath = “C:\myproject\project.exe”
$iconPath = “C:\myproject\file.ico”
$Shortcut.Save()

in your example code you’re setting the icon path to a variable and then never using that variable.
The example1 in the documentation covers how to do this.
https://learn.microsoft.com/en-us/troubleshoot/windows-client/admin-development/create-desktop-shortcut-with-wsh

2 Likes

Hi Theyikes,

Here’s a condensed snippet of a function I wrote for this:

# Truncated...

$Path = $env:USERPROFILE + "\Desktop"
$LogoutExecutable = $env:windir + "\System32\logoff.exe"
$IconLoc = $env:windir + "\System32\SHELL32.dll,104"

$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Path\Logout.lnk")
$Shortcut.TargetPath = $LogoutExecutable
$Shortcut.IconLocation = $IconLoc
$Shortcut.Save()

# Truncated...

The above is for a logout shortcut, but as you know the path can be your .exe’s path. The same goes with the shortcut’s icon. Hope this helps!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.