PowerShell/Scripts/create-shortcut.ps1

36 lines
983 B
PowerShell
Raw Normal View History

2021-04-21 19:53:52 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
create-shortcut.ps1 [<shortcut>] [<target>] [<description>]
.DESCRIPTION
Creates a new shortcut file
.EXAMPLE
PS> .\create-shortcut.ps1 C:\Temp\HDD C:\
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
2021-08-03 15:53:57 +02:00
Author: Markus Fleschutz
License: CC0
2021-03-15 08:06:37 +01:00
#>
2021-08-22 18:45:05 +02:00
param([string]$shortcut = "", [string]$target = "", [string]$description)
2021-03-15 08:06:37 +01:00
try {
2021-08-22 18:45:05 +02:00
if ($shortcut -eq "" ) { $shortcut = read-host "Enter new shortcut filename" }
if ($target -eq "" ) { $target = read-host "Enter path to target" }
if ($description -eq "" ) { $description = read-host "Enter description" }
2021-07-15 15:51:22 +02:00
2021-03-15 08:06:37 +01:00
$sh = new-object -ComObject WScript.Shell
2021-08-22 18:45:05 +02:00
$sc = $sh.CreateShortcut("$shortcut.lnk")
$sc.TargetPath = "$target"
$sc.WindowStyle = "1"
$sc.IconLocation = "C:\Windows\System32\SHELL32.dll, 3"
$sc.Description = "$description"
$sc.save()
2021-03-15 08:06:37 +01:00
2021-08-22 18:45:05 +02:00
"✔️ created shortcut $shortcut$target"
2021-03-15 08:06:37 +01:00
exit 0
} catch {
2021-05-02 21:30:48 +02:00
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-03-15 08:06:37 +01:00
exit 1
}