Public/New-pseDesktopShortcut.ps1
function New-pseDesktopShortcut { <# .SYNOPSIS Creates a new desktop shortcut. .DESCRIPTION This function creates a shortcut on the desktop with the specified properties. You can run it multiple times and it will overwrite the existing shortcut. .PARAMETER ShortcutName The name of the shortcut (excluding the .lnk extension). .PARAMETER TargetPath The target path that the shortcut points to. .PARAMETER WorkingDirectory The working directory for the shortcut. .PARAMETER Arguments The command-line arguments for the target application. .PARAMETER IconLocation The icon for the shortcut in the format 'c:\path\to\icon.ico'. .PARAMETER WindowStyle The window style for the shortcut (1 - Normal, 3 - Maximized, 7 - Minimized). .PARAMETER AllUsers Switch parameter. If present, the shortcut will be created for all users. .EXAMPLE New-pseDesktopShortcut -ShortcutName "MyApp" -TargetPath "C:\Path\To\MyApp.exe" -WorkingDirectory "C:\Path\To" -Arguments "-arg1 value1 -arg2 value2" -IconLocation "MyIcon.dll,0" -WindowStyle 1 Creates a desktop shortcut named "MyApp" with specified parameters for the current user. .EXAMPLE $shortcutParams = [pscustomobject]@{ ShortcutName = 'Tonoref' TargetPath = $shortcutURL IconLocation = "$env:SystemRoot\system32\$icon" WindowStyle = 3 AllUsers = $true } $shortcutParams | New-pseDesktopShortcut Creates a desktop shortcut named "Tonoref" with specified parameters for all users. .NOTES Author: owen.heaume Version: 1.0.0 - Initial release #> [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string[]]$ShortcutName, [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [string]$TargetPath, [Parameter(ValueFromPipelineByPropertyName = $true)] [string]$WorkingDirectory, [Parameter(ValueFromPipelineByPropertyName = $true)] [string]$Arguments, [Parameter(ValueFromPipelineByPropertyName = $true)] [string]$IconLocation = 'imageres.dll,4', [Parameter(ValueFromPipelineByPropertyName = $true)] [ValidateSet(1, 3, 4, 7)] $WindowStyle = 7, [Parameter(ValueFromPipelineByPropertyName = $true)] [switch]$AllUsers ) begin { } Process { foreach ($name in $ShortcutName) { # Expand environment variables $ExpandedTargetPath = $ExecutionContext.InvokeCommand.ExpandString($TargetPath) $ExpandedWorkingDirectory = $ExecutionContext.InvokeCommand.ExpandString($WorkingDirectory) $ExpandedArguments = $ExecutionContext.InvokeCommand.ExpandString($Arguments) # Create a WScript.Shell object $WshShell = New-Object -ComObject WScript.Shell # Determine the desktop path if ($AllUsers.IsPresent) { $desktopPath = [Environment]::GetFolderPath("CommonDesktopDirectory") } else { $desktopPath = [Environment]::GetFolderPath("Desktop") } # If the icon location is not specified, use the default icon if ([string]::IsNullOrEmpty($IconLocation)) { $IconLocation = 'imageres.dll,4' } # If the WindowStyle is not specified, use 7 if ([string]::IsNullOrEmpty($WindowStyle)) { $WindowStyle = 7 } # Create the shortcut object $shortcut = $WshShell.CreateShortcut("$desktopPath\$Name.lnk") try { # Set properties $shortcut.TargetPath = $ExpandedTargetPath $shortcut.Arguments = $ExpandedArguments $shortcut.WorkingDirectory = $ExpandedWorkingDirectory $shortcut.IconLocation = $IconLocation $shortcut.WindowStyle = $WindowStyle $shortcut.Save() Write-Host "Created shortcut: $desktopPath\$Name.lnk" -ForegroundColor DarkGray } catch { Write-Error $_.Exception.Message } } } } |