如何调整PowerShell脚本逻辑,借助Get-StartApps生成含.URL的动态开始菜单
Step 1: Fetch Target Entries (Combine Get-StartApps + Web Shortcuts)
First, a quick heads-up: Get-StartApps primarily pulls registered desktop/UWP apps, not loose .URL/.website shortcuts stored in Start Menu folders. To cover both (while replacing your recursive file filter), we’ll combine the command with a targeted search for those web shortcut files—this keeps things efficient and aligned with your goal:
# Grab standard registered apps via Get-StartApps $registeredApps = Get-StartApps # Get .URL and .website shortcuts from user/system Start Menu folders $startMenuLocations = @( [Environment]::GetFolderPath('StartMenu'), "$env:ProgramData\Microsoft\Windows\Start Menu\Programs" ) $webShortcuts = Get-ChildItem -Path $startMenuLocations -Include *.url,*.website -Recurse -File | ForEach-Object { [PSCustomObject]@{ DisplayName = $_.BaseName Target = $_.FullName EntryType = 'WebShortcut' } } # Merge both sets of entries into a single list $allStartEntries = $registeredApps | ForEach-Object { [PSCustomObject]@{ DisplayName = $_.Name Target = $_.AppID EntryType = 'StandardApp' } } + $webShortcuts
Step 2: Rewrite XML Generation Logic (Section #3)
Your original XML script likely assumes standard app IDs, so we need to adjust it to handle web shortcuts by generating the correct shell execution commands. Replace your #3 section with this tailored logic:
# Generate XML entries for each item $xmlEntryList = $allStartEntries | ForEach-Object { if ($_.EntryType -eq 'StandardApp') { # Keep your existing logic for registered apps "<MenuItem Name=`"$($_.DisplayName)`" AppID=`"$($_.Target)`" />" } else { # Handle .URL/.website: Use explorer.exe to open the shortcut natively $escapedPath = [System.Security.SecurityElement]::Escape($_.Target) "<MenuItem Name=`"$($_.DisplayName)`" Command=`"explorer.exe`" Args=`"$escapedPath`" />" } } # Assemble the full XML structure $finalStartMenuXml = @" <DynamicStartMenu> $($xmlEntryList -join "`n ") </DynamicStartMenu> "@ # Save or output the XML $finalStartMenuXml | Out-File -Path "CustomStartMenu.xml" -Encoding UTF8
Critical Tips for Web Shortcut Handling
- Path Escaping: Always use
[System.Security.SecurityElement]::Escape()to sanitize file paths for XML—this prevents invalid characters like&or<from breaking your XML structure. - Native Execution: Web shortcuts rely on
explorer.exeto trigger their default handler (your browser), so directly referencing the shortcut path won’t work in most Start Menu XML setups. - Filtering Options: If you want to narrow down
Get-StartAppsresults, add aWhere-Objectclause, e.g.,Get-StartApps | Where-Object { $_.Name -match 'Web|Browser' }to target web-focused apps.
Troubleshooting Quick Hits
- If
Get-StartAppsmisses apps, double-check that their.lnkshortcuts exist in the Start Menu folders. - For
.websitefiles (which are often XML-based themselves),explorer.exewill handle them natively—no extra parsing needed.
内容的提问来源于stack exchange,提问作者Powershelling
相关产品推荐
相关产品推荐

