Auch Verknüpfungen oder shortcuts kann man bequem per Powershell erstellen oder auch Löschen.
- Details
- Geschrieben von Super User
- Kategorie: Powershell

#------------------------------------------------------------------------------------------
# Name: ShortCuts.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.
# IconId = The id of the icon in the IconLocation file
# Usage: .\ShortCuts.ps1 -Name CommandLine -Target c:\windows\System32\cmd.exe -Destinations "$env:PUBLIC\Desktop\,C:\ProgramData\Microsoft\Windows\Start Menu\Programs\" -IconLocation "C:\Windows\System32\shell32.dll" -IconId 24 -Mode create
# Usage: .\ShortCuts.ps1 -Name CommandLine -Target c:\windows\System32\cmd.exe -Destinations "$env:PUBLIC\Desktop\,C:\ProgramData\Microsoft\Windows\Start Menu\Programs\" -IconLocation "C:\Windows\System32\shell32.dll" -IconId 24 -Mode remove
#------------------------------------------------------------------------------------------
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."}
}
- Zugriffe: 9209