PowerShell统计注册表条目获取Edge扩展安装列表下一条序号的方法
错误原因
你赋值给$Value的不是整数值,是一个包含Count属性的自定义对象,所以直接强转int会触发类型不匹配报错。
正确实现步骤
1. 正确获取注册表条目数(兼容路径不存在的情况)
直接取Measure-Object返回结果的Count属性即可拿到整数类型的条目数:
# 目标注册表路径,可替换为你实际要用的ExtensionInstallForcelist路径 $regPath = "SOFTWARE\Policies\Microsoft\Edge\ExtensionInstallBlocklist" # 判断注册表路径是否存在,不存在则条目数为0 if (Test-RegKey -Hive LocalMachine -Key $regPath) { $entryCount = (Get-RegValue -Hive LocalMachine -Key $regPath | Measure-Object).Count } else { $entryCount = 0 }
如果你的环境没有安装PSRegistry模块(Get-RegValue属于该模块),也可以用PowerShell原生命令实现,兼容性更好:
$regPath = "HKLM:\SOFTWARE\Policies\Microsoft\Edge\ExtensionInstallBlocklist" if (Test-Path $regPath) { $entryCount = (Get-ItemProperty $regPath | Select-Object * -ExcludeProperty PSPath,PSParentPath,PSChildName,PSDrive,PSProvider | Measure-Object).Count } else { $entryCount = 0 }
2. 生成新条目的序号
直接对整数类型的$entryCount做加法即可:
$newEntryIndex = $entryCount + 1
3. 写入新扩展条目示例
以写入Edge强制安装扩展为例:
# 替换为你要安装的扩展ID和更新地址 $extensionValue = "abcdefghijklmnopqrstuvwxyz;https://edge.microsoft.com/extensionwebstorebase/v1/crx" # 路径不存在则先创建 if (-not (Test-Path $regPath)) { New-Item -Path $regPath -Force | Out-Null } # 写入新条目 Set-ItemProperty -Path $regPath -Name $newEntryIndex.ToString() -Value $extensionValue -Type String
注意事项
- 修改
HKLM注册表路径需要以管理员身份运行PowerShell,否则会提示权限不足 - 若需要同时适配Chrome,只需将注册表路径替换为Chrome对应的
SOFTWARE\Policies\Google\Chrome\ExtensionInstallForcelist即可
内容的提问来源于stack exchange,提问作者ThrowAway
相关产品推荐
相关产品推荐

