0

I have a powershell script that should make a shortcut to a image on the desktop, however, when I run the script, I get the error:

Unable to save shortcut "C:\users\muffin\desktop\a.png C:\users\muffin\Desktop\testshortcut.lnk".
At C:\Users\muffin\Downloads\setup.ps1:10 char:9
+         $Shortcut.Save()
+         ~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException

my code is as follows:

function Set-Shortcut {
    param ( [string]$SourceLnk, [string]$DestinationPath )
        $WshShell = New-Object -comObject WScript.Shell
        $Shortcut = $WshShell.CreateShortcut($SourceLnk)
        $Shortcut.TargetPath = $DestinationPath
        $Shortcut.Save()
}

Set-Shortcut("C:\users\muffin\documents\a.png", "C:\users\muffin\Desktop\testshortcut.lnk")

I have checked that the file exists, and I'm fairly sure that I haven't made any spelling errors.

Gurnoor
  • 103
  • 1
  • 6
  • 2
    [Classic PowerShell Gotcha (#2)](https://stackoverflow.com/a/69644807/1701026): `Set-Shortcut "C:\users\muffin\documents\a.png" "C:\users\muffin\Desktop\testshortcut.lnk"` – iRon May 20 '22 at 08:23

0 Answers0