Windows 11下使用PowerShell取消固定开始菜单应用失败问题求助
Windows 11下使用PowerShell取消固定开始菜单应用失败问题求助
嗨,我来帮你分析下问题所在:
你遇到的「不能对空值表达式调用方法」错误,本质原因是你用来查找开始菜单应用的Shell命名空间不对!
你在两个函数里都用了shell:::{4234d49b-0245-4df3-b780-3893943456e1}这个路径,但这个路径对应的是Windows任务栏的跳转列表文件夹,只会包含当前固定在任务栏上的项目。而ESPN、Spotify是固定在开始菜单的,根本不在这个文件夹里,所以Where-Object找不到匹配项,返回了$null,后续调用.Verbs()自然就报错了。
解决方案:使用正确的开始菜单固定项命名空间
Windows 11里有专门的Shell命名空间用来访问「开始菜单固定项目」,路径是shell:::{9AA66D95-763B-4207-A8E3-8674D08C6C2D}。我给你修正后的代码,同时增加了错误判断,避免空值报错:
function Unpin-FromTaskbar { param([string]$AppName) # 任务栏项目的Shell命名空间 $taskbarFolder = (New-Object -ComObject Shell.Application).Namespace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}') $appItem = $taskbarFolder.Items() | Where-Object { $_.Name -eq $AppName } if (-not $appItem) { Write-Host "⚠️ $AppName 不在任务栏固定项中" return } $unpinVerb = $appItem.Verbs() | Where-Object { $_.Name.Replace('&', '') -eq 'Unpin from taskbar' } if ($unpinVerb) { $unpinVerb.DoIt() Write-Host "✅ 已成功取消 $AppName 任务栏固定" } else { Write-Host "ℹ️ $AppName 当前未固定在任务栏" } } function Unpin-FromStartMenu { param([string]$AppName) # 开始菜单固定项的Shell命名空间 $startPinnedFolder = (New-Object -ComObject Shell.Application).Namespace('shell:::{9AA66D95-763B-4207-A8E3-8674D08C6C2D}') $appItem = $startPinnedFolder.Items() | Where-Object { $_.Name -eq $AppName } if (-not $appItem) { Write-Host "⚠️ $AppName 不在开始菜单固定项中" return } $unpinVerb = $appItem.Verbs() | Where-Object { $_.Name.Replace('&', '') -eq 'Unpin from Start' } if ($unpinVerb) { $unpinVerb.DoIt() Write-Host "✅ 已成功取消 $AppName 开始菜单固定" } else { Write-Host "ℹ️ $AppName 当前未固定在开始菜单" } } # 执行取消固定操作 Unpin-FromTaskbar -AppName 'Microsoft Store' foreach ($app in 'ESPN', 'Spotify') { Unpin-FromStartMenu -AppName $app }
额外注意事项
如果还是找不到ESPN或Spotify,大概率是应用的显示名称和你写的不一致。你可以打开下面的路径,查看开始菜单固定项的准确名称:
explorer.exe shell:::{9AA66D95-763B-4207-A8E3-8674D08C6C2D}
比如ESPN可能显示为「ESPN: Live Sports & Scores」,你需要把代码里的'ESPN'替换成这个完整名称。
备注:内容来源于stack exchange,提问作者aristosv
相关产品推荐
相关产品推荐

