Mir ist aktuell keine direkte Methode bekannt, mit der man mit einem einzigen Befehl eine Verknüpfung mit Parametern in Powershell erstellen kann. Jedoch kann man Objekte wie das WScript.Shall auch in PowerShell benutzen. Ich erinne mich zumindest wage, dass es in diesem Bereich eine Neuerung gab, konnte aber nichts finden. Hier nun dennoch die ultimativen Powershell Funktionen zur Erstellung von Shortcus. Dabei u.a. auch die Möglichkeit, administrative Shortcuts zu erzeugen, die eine Elevation der Anwendung bewirken. Als Parameter kann ein "ico" Icon File angegeben werden und ansonsetn wird versucht, mit den "Special Folders" zu arbeiten.
Praktisch ist hierbei insbesondere der Befehl „SpecialFolders“, mit dem die Lokation der Verknüpfung automatisch festgelegt werden kann.
- AllUsersDesktop
- AllUsersStartMenu
- AllUsersPrograms
- AllUsersStartup
- Desktop
- Favorites
- SendTo
- StartMenu
- Startup #Autostart
Powershell Create Shortcut Funktion:
<# .SYNOPSIS Create a Shortcut .DESCRIPTION Create a link based a a spacial folder name .PARAMETER ShortcutFilePath relative Path to the SpecialFolder "\appfolder\myapp.lnk" oder simpley "myapp.lnk" example LinkLocation = Desktop: ..\Desktop\appfolder\myapp.lnk" .PARAMETER linklocation ;andatory, a SpecialFolder name "Desktop","StartMenu" etc. .PARAMETER TargetPath Mandatory, Path to a exe or batch file etc. .PARAMETER Hotkey Optional, a Hotkey to start. Example : "CTRL+F" .PARAMETER WindowsStyle Optional 1 - Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. 3 - Activates the window and displays it as a maximized window. 7 - Minimizes the window and activates the next top-level window. .PARAMETER IconLocation Path to the icon file and Icon Number. Example: "C:\Windows\system32\shell32.dll, 1" .PARAMETER Arguments Optional, command line arguments. Example: /c echo hallo .PARAMETER WorkingDirectory Optional, execution path of the script or exe .PARAMETER $Description Optional, a description of your shortcut .PARAMETER UACAdmin Optinal, run the shortcut as administrator .EXAMPLE Create a administrative shortcut for notepad.exe in the folder lalula on the Desktop Create-Shortcut -ShortcutFilePath "lalula\test2.lnk" -linklocation Desktop -TargetPath c:\Windows\notepad.exe -UACAdmin:$true .AUTOR Andreas Nick' 2016 #> [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [String]$ShortcutFilePath, [ValidateSet("AllUsersDesktop", "AllUsersStartMenu", "AllUsersPrograms", "AllUsersStartup", "Desktop", "Favorites", "SendTo", "StartMenu", "Startup")] [Parameter(Mandatory = $true)] [String]$linklocation, [Parameter(Mandatory = $true)] [string]$TargetPath, [String]$Hotkey = "", [ValidateSet("1", "3", "7")] [String]$WindwosStyle = "1", [String]$IconLocation = "", [String]$Arguments = "", [String]$WorkingDirectory = "", [String]$Description = "", [switch]$UACAdmin = $false ) process { write-host "Test" If (-not ($ShortcutFilePath -match "\.lnk$")) { Write-Verbose $('Error: Illigal Shortcut' + "$ShortcutFilePath"+ ' - a shortcut ends with .lnk') throw 'Error: Illigal Shortcut' + "$ShortcutFilePath"+ ' - a shortcut ends with .lnk' } [System.IO.FileInfo] $DestinationPath = [environment]::getfolderpath("$linklocation") + '\' + $ShortcutFilePath #Check The Path if (-not (test-path (Split-Path $DestinationPath.FullName -Parent))) { new-item -Type Directory -Path (Split-Path $DestinationPath.FullName -Parent) } #Create a new shortcut for computers or users. $WshShell = New-Object -ComObject WScript.Shell $Shortcut = $WshShell.CreateShortcut($DestinationPath.FullName) $Shortcut.TargetPath = "$TargetPath" $Shortcut.Arguments = $Arguments $Shortcut.Description = $Description $Shortcut.HotKey = $HotKey $Shortcut.WorkingDirectory = $WorkingDirectory $Shortcut.WindowStyle = $WindwosStyle if ($IconLocation -eq "") { $Shortcut.IconLocation = "C:\Windows\system32\shell32.dll, 17" } else { $Shortcut.IconLocation = $IconLocation } try { $Shortcut.Save() if ($UACAdmin) { [byte[]]$bytes = [System.IO.File]::ReadAllBytes("$DestinationPath") $bytes[0x15] = $bytes[0x15] -bor 0x20 [System.IO.File]::WriteAllBytes("$DestinationPath", $bytes) } } Catch { Write-Verbose "Cannot create the shortcut $($DestinationPath.FullName)" -Verbose Write-Verbose $Error[0].Exception.Message -Verbose Return $False } }
Hier noch einmal die gleiche Funktion für Web-Shortcuts:
<# .SYNOPSIS Create a Shortcut .DESCRIPTION Create a link based a a spacial folder name .PARAMETER ShortcutFilePath relative Path to the SpecialFolder "\appfolder\myapp.lnk" oder simpley "myapp.lnk" example LinkLocation = Desktop: ..\Desktop\appfolder\myapp.lnk" .PARAMETER linklocation ;andatory, a SpecialFolder name "Desktop","StartMenu" etc. .PARAMETER TargetPath Mandatory, Path to a exe or batch file etc. .PARAMETER Hotkey Optional, a Hotkey to start. Example : "CTRL+F" .PARAMETER IconLocation Path to the icon file and Icon Number. Example: "C:\Windows\system32\shell32.dll, 1" .PARAMETER $Description Optional, a description of your shortcut .EXAMPLE Create-WebShortcut -ShortcutFilePath "heise.lnk" -linklocation Desktop -TargetPath http://www.heise.de .AUTOR Andreas Nick' 2016 #> function Create-WebShortcut { #[CmdletBinding()] param ( [Parameter(Mandatory = $true)] [String]$ShortcutFilePath, [ValidateSet("AllUsersDesktop", "AllUsersStartMenu", "AllUsersPrograms", "AllUsersStartup", "Desktop", "Favorites", "SendTo", "StartMenu", "Startup")] [Parameter(Mandatory = $true)] [String]$linklocation, [Parameter(Mandatory = $true)] [string]$TargetPath, [String]$Hotkey = "", [String]$IconLocation = "C:\Program Files\Internet Explorer\iexplore.exe, 0", [String]$Description = "" ) process { If (-not ($ShortcutFilePath -match "\.lnk$")) { Write-Verbose $('Error: Illigal Shortcut' + "$ShortcutFilePath" + ' - a shortcut ends with .lnk') throw 'Error: Illigal Shortcut' + "$ShortcutFilePath" + ' - a shortcut ends with .lnk' } [System.IO.FileInfo]$DestinationPath = [environment]::getfolderpath("$linklocation") + '\' + $ShortcutFilePath #Check The Path if (-not (test-path (Split-Path $DestinationPath.FullName -Parent))) { new-item -Type Directory -Path (Split-Path $DestinationPath.FullName -Parent) } #Create a new shortcut for computers or users. $WshShell = New-Object -ComObject WScript.Shell $Shortcut = $WshShell.CreateShortcut($DestinationPath.FullName) $Shortcut.TargetPath = "$TargetPath" $Shortcut.IconLocation = $IconLocation $Shortcut.Description = $Description $Shortcut.HotKey = $HotKey try { $Shortcut.Save() } Catch { Write-Verbose "Cannot create the web shortcut $($DestinationPath.FullName)" -Verbose Write-Verbose $Error[0].Exception.Message -Verbose Return $False } } }