Powershell Shortcuts erstellen

Erstellt am 21. Oktober 2016

Mit diesem Script lassen sich Shortcuts in einem oder mehreren Verzeichnissen gleichzeitig erstellen.

#------------------------------------------------------------------------------------------
# Name: createRemoveLnk.ps1
# expected parameters:
# Name = The name of the shortcut
# Target = The path an file the shortcut should point to
# Destination = The location where the shortcut has to be created
# Mode = create | remove
# Argument = Additional argument for the shortcut
# IconLocation = path & file name of file that provides the desired icon.
#  Example: C:\Windows\System32\shell32.dll
# IconId = The id of the icon in the IconLocation file
#------------------------------------------------------------------------------------------
param (
 [parameter(Mandatory=$true,Position=1)]
 [string]$Name,
 [parameter(Mandatory=$false)]
 [AllowNull()]
 [string]$Target,
 [parameter(Mandatory=$true)]
 [AllowNull()]
 [string]$Destinations,
 [parameter(Mandatory=$true)]
 [string]$Mode,
 [parameter(Mandatory=$false)]
 [AllowNull()]
 [string]$Argument,
 [parameter(Mandatory=$false)]
 [AllowNull()]
 [string]$IconLocation,
 [parameter(Mandatory=$false)]
 [AllowNull()]
 [string]$IconId
)
function create-Shortcut($lnkName,$lnkDestination,$lnkTarget,$lnkArgument,$IconLocation,$IconId)
{
  $lnkDestination = $lnkDestination.split(",")
  foreach ($dir in $lnkDestination)
  {
     #create the shortcut object
     $WshShell = New-Object -ComObject WScript.Shell
     $Shortcut = $WshShell.CreateShortcut("$($dir)\$($lnkName).lnk")
     #program the shortcut will open
     $Shortcut.TargetPath = $lnkTarget
     #icon location & Id that the shortcut will use
     $Shortcut.IconLocation = "$IconLocation,$IconId"
     #any extra parameters that the shortcut may have
     $Shortcut.Arguments = "$lnkArgument"
     #save the modifications
     $Shortcut.Save()
  }
}
function remove-Shortcut($lnkName,$lnkDestination)
{
  $lnkDestination = $lnkDestination.split(",")
  foreach ($dir in $lnkDestination)
  {
    if (Test-Path "$dir")
    {
      #remove the shortcut if uninstalled
      Remove-Item -Path "$dir\$lnkName.lnk"
    }
  }
}
switch ($Mode)
{
  "create"
  {
    create-Shortcut -lnkName $Name -lnkDestination $Destinations -lnkTarget $Target -lnkArgument $Argument -IconLocation $IconLocation -IconId $IconId
  }
  "remove"
  {
    remove-Shortcut -lnkName $Name -lnkDestination $Destinations
  }
  default{"You did not enter a valid parameter. Mode can be install or uninstall only."}
}

Beispiel 1 – Erstellen eines Shortcuts

Hiermit wird ein Shortcut namens „MyShortcut“ sowohl auf dem Desktop als auch direkt im Start Menu erstellt. Dieser Shortcut öffnet Notepad.exe. Es wird das 7. Icon wird aus der Shell32.dll verwendet (beginnend bei 0).

.\createRemoveLnk.ps1 -Name MyShortcut -Target C:\Windows\notepad.exe -Destinations "$env:PUBLIC\Desktop,C:\ProgramData\Microsoft\Windows\Start Menu" -IconLocation "C:\Windows\System32\shell32.dll" -IconId 6 -Mode create

Beispiel 2 – Löschen eines Shortcuts

Hiermit wird der zuvor erstellte Shortcut wieder gelöscht.

.\createRemoveLnk.ps1 -Name MyShortcut -Destinations "$env:PUBLIC\Desktop,C:\ProgramData\Microsoft\Windows\Start Menu" -Mode "remove"

Soll nur der Shortcut auf dem Desktop gelöscht werden, lässt man einfach den Start Menu Pfad weg.

.\createRemoveLnk.ps1 -Name MyShortcut -Destinations "$env:PUBLIC\Desktop" -Mode "remove"

Getestet mit Powershell 5.0

Erstellt am 21. Oktober 2016

Vielleicht auch interessant…

MSIX – Was heisst das für mein Team? (Teil 2)

MSIX – Was heisst das für mein Team? (Teil 2)

Herzlich willkommen zur MSIX-Blogserie, in der ich für unsere Leserinnen und Leser eine Übersicht über MSIX, bekannterweise das neu lancierte Installationsformat von Microsoft, bereitstelle. Der erste Teil der Serie ist hier nachzulesen, ich wünsche viel Vergnügen....

mehr lesen

This Area is Widget-Ready

You can place here any widget you want!

You can also display any layout saved in Divi Library.

Let’s try with contact form: