Snippets

Stephen Valdinger New Desktop Shortcut

Updated by Stephen Valdinger

File New-DesktopShortcut.ps1 Modified

  • Ignore whitespace
  • Hide word diff
 
         .PARAMETER ShortcutPath
         The path name of the new Shortcut.
+        
+        .PARAMETER IconPath
+        The full path to an icon for the shortcut.
 
         .EXAMPLE
         New-DesktopShortcut -TargetExecutable "C:\Program Files(x86)\SuperSoftware\SuperCool.exe" -ShortcutPath = "$env:Public\Desktop\SuperCool.lnk"
+        
+        .EXAMPLE
+        New-DesktopShortcut -TargetExecutable "C:\Program Files(x86)\SuperSoftware\SuperCool.exe" -ShortcutPath = "$env:Public\Desktop\SuperCool.lnk" -IconPath 'C:\icons\fancy.ico'
     
     #>
     [cmdletBinding()]
Updated by Stephen Valdinger

File New-DesktopShortcut.ps1 Modified

  • Ignore whitespace
  • Hide word diff
         [Parameter(Mandatory, Position = 0)]
         [String]$Target,
         [parameter(Mandatory, Position = 1)]
-        [String]$ShortcutPath
+        [String]$ShortcutPath,
+        [Paramter(Mandatory = $false, Position = 2)]
+        [String]$IconPath
         
     )
 
     $DesktopShortcut = $Shell.CreateShortcut($ShortcutPath)
     $DesktopShortcut.TargetPath = $Target
     $DesktopShortcut.WorkingDirectory = Split-Path -Path $Target
+    
+    If($IconPath){
+        
+        $DesktopShortcut.IconLocation($IconPath,0)
+    
+    }
+    
     $DesktopShortcut.Save()
 
 }
Updated by Stephen Valdinger

File New-DesktopShortcut.ps1 Modified

  • Ignore whitespace
  • Hide word diff
     $Shell = New-Object -ComObject Wscript.Shell
     $DesktopShortcut = $Shell.CreateShortcut($ShortcutPath)
     $DesktopShortcut.TargetPath = $Target
+    $DesktopShortcut.WorkingDirectory = Split-Path -Path $Target
     $DesktopShortcut.Save()
 
 }
Created by Stephen Valdinger

File New-DesktopShortcut.ps1 Added

  • Ignore whitespace
  • Hide word diff
+Function New-DesktopShortcut {
+    <#
+        .SYNOPSIS
+        Creates a Desktop Shortcut in a User's profile
+
+        .PARAMETER Target
+        [String]
+        The full path of the executable/file to which you are creating a shortcut.
+
+        .PARAMETER ShortcutPath
+        The path name of the new Shortcut.
+
+        .EXAMPLE
+        New-DesktopShortcut -TargetExecutable "C:\Program Files(x86)\SuperSoftware\SuperCool.exe" -ShortcutPath = "$env:Public\Desktop\SuperCool.lnk"
+    
+    #>
+    [cmdletBinding()]
+    Param(
+        
+        [Parameter(Mandatory, Position = 0)]
+        [String]$Target,
+        [parameter(Mandatory, Position = 1)]
+        [String]$ShortcutPath
+        
+    )
+
+    $Shell = New-Object -ComObject Wscript.Shell
+    $DesktopShortcut = $Shell.CreateShortcut($ShortcutPath)
+    $DesktopShortcut.TargetPath = $Target
+    $DesktopShortcut.Save()
+
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.