You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整PowerShell脚本逻辑,借助Get-StartApps生成含.URL的动态开始菜单

Solution: Dynamic Start Menu with .URL/.website Using Get-StartApps

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.exe to 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-StartApps results, add a Where-Object clause, e.g., Get-StartApps | Where-Object { $_.Name -match 'Web|Browser' } to target web-focused apps.

Troubleshooting Quick Hits

  • If Get-StartApps misses apps, double-check that their .lnk shortcuts exist in the Start Menu folders.
  • For .website files (which are often XML-based themselves), explorer.exe will handle them natively—no extra parsing needed.

内容的提问来源于stack exchange,提问作者Powershelling

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:29:11