Skip to content

Commit 440601e

Browse files
committed
Add shortcut creation functionality to manage Start Menu entries
1 parent d4776bf commit 440601e

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

Sources/Winget-AutoUpdate/mods/_AppID-template.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ $AllVersions = $False
5151
# Beginning of Desktop Link Name to Remove - optional wildcard (*) after, without .lnk, multiple: "lnk1*","lnk2"
5252
$Lnk = @("")
5353

54-
# Create Start Menu Shortcuts, without .lnk, multiple: "lnk1","lnk2"
54+
# Create Start Menu Shortcuts, without .lnk, multiple: "lnk1","lnk2". Example:
55+
# $Shortcuts = @("dnGrep\dnGrep")
56+
# $ShortcutsTargets = @("${env:ProgramFiles}\dnGrep\dnGrep.exe")
5557
$Shortcuts = @("")
5658
$ShortcutsTargets = @("") # Must match the order of $Shortcuts
5759

@@ -134,7 +136,7 @@ if ($Lnk) {
134136
Remove-ModsLnk $Lnk
135137
}
136138
if ($Shortcuts) {
137-
Add-Shortcuts $Shortcuts $ShortcutsTargets
139+
Add-ProgramsShortcuts $Shortcuts $ShortcutsTargets
138140
}
139141
if ($AddKey -and $AddValue -and $AddTypeData -and $AddType) {
140142
Add-ModsReg $AddKey $AddValue $AddTypeData $AddType

Sources/Winget-AutoUpdate/mods/_Mods-Functions.ps1

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,54 @@ function Remove-ModsLnk ($Lnk) {
185185
Return $removedCount
186186
}
187187

188+
function Add-ProgramsShortcuts ($Shortcuts, $ShortcutsTargets) {
189+
$programsPath = "${env:ProgramData}\Microsoft\Windows\Start Menu\Programs"
190+
$createdCount = 0
191+
192+
# Validate arrays match in length
193+
if ($Shortcuts.Count -ne $ShortcutsTargets.Count) {
194+
Return $createdCount
195+
}
196+
197+
# Create WScript.Shell COM object
198+
$WshShell = New-Object -ComObject WScript.Shell -ErrorAction SilentlyContinue
199+
if (!$WshShell) {
200+
Return $createdCount
201+
}
202+
203+
# Iterate through shortcuts
204+
for ($i = 0; $i -lt $Shortcuts.Count; $i++) {
205+
$shortcutName = $Shortcuts[$i]
206+
$targetPath = $ShortcutsTargets[$i]
207+
208+
# Skip empty entries
209+
if ([string]::IsNullOrEmpty($shortcutName) -or [string]::IsNullOrEmpty($targetPath)) {
210+
continue
211+
}
212+
213+
# Construct full shortcut path
214+
$shortcutPath = Join-Path $programsPath "$shortcutName.lnk"
215+
216+
# Create parent directory if it doesn't exist
217+
$shortcutDir = Split-Path $shortcutPath -Parent
218+
if (!(Test-Path $shortcutDir)) {
219+
New-Item -Path $shortcutDir -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null
220+
}
221+
222+
# Verify target exists
223+
if (Test-Path $targetPath) {
224+
# Create shortcut
225+
$shortcut = $WshShell.CreateShortcut($shortcutPath)
226+
$shortcut.TargetPath = $targetPath
227+
$shortcut.WorkingDirectory = Split-Path $targetPath -Parent
228+
$shortcut.Save()
229+
$createdCount++
230+
}
231+
}
232+
233+
Return $createdCount
234+
}
235+
188236
function Add-ModsReg ($AddKey, $AddValue, $AddTypeData, $AddType) {
189237
if ($AddKey -like "HKEY_LOCAL_MACHINE*") {
190238
$AddKey = $AddKey.replace("HKEY_LOCAL_MACHINE", "HKLM:")

0 commit comments

Comments
 (0)