Powershell - 删除注册表项中的所有属性

标签 powershell registry

以下函数实际上可以解决问题:

function Remove-AllItemProperties([String] $path) {
    Get-ItemProperty $path | Get-Member -MemberType Properties | Foreach-Object {
        if (("PSChildName","PSDrive","PSParentPath","PSPath","PSProvider") -notcontains $_.Name) {
            Remove-itemproperty -path $path -Name $_.Name
        }
    }
}

例如:要从注册表中删除所有键入的 url,您可以使用
Remove-AllItemProperties("HKCU:\SOFTWARE\Microsoft\Internet Explorer\TypedURLs")

我的问题是:
  • 由于我对 Powershell 比较陌生:我想知道是否有更漂亮的(即问题的紧凑解决方案。
  • )
  • 如果项目(注册表项)没有属性(Get-Member 提示缺少对象),这些函数会抛出错误。

  • 谢谢你的想法!

    最佳答案

    I wonder if there is not a more beautiful (i.e. compact solution for the problem.



    我会简单地使用 Remove-ItemProperty -Name * :
    function Remove-AllItemProperties
    {
        [CmdletBinding()]
        param([string]$Path)
    
        Remove-ItemProperty -Name * @PSBoundParameters
    }
    
    Remove-ItemProperty -Name *将删除注册表项中的任何现有值 $Path .
    [CmdletBinding()]属性会自动将通用参数( -Verbose-Debug-ErrorAction 等)添加到您的函数中。

    通过喷溅 $PSBoundParameters到内部调用,您会自动将这些选项直接传递给 Remove-ItemProperty

    The functions throws an error if the item (registry key) has no properties (Get-Member complains about a missing object).



    上面的方法不会有这个问题

    关于Powershell - 删除注册表项中的所有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38557634/

    相关文章:

    arrays - 如何动态引用Powershell变量

    c# - 通过nuget包添加代码分析规则集

    powershell - 多线程 PowerShell 脚本可更快地从大型 XML 文件中提取数据

    registry - 使用 Yarn 从 Github 包注册表安装私有(private)包失败,未经授权

    c++ - 如何在Windows中模拟注册表进行程序测试?

    powershell - $MyInspiration.MyCommand.Name 提供奇怪的结果

    Python argparse 无法正确处理 Windows 中的路径

    windows - 如何将自定义协议(protocol)添加到 Vista "Set Associations"列表?

    vba - 在 VBA 中读写注册表

    registry - 在 VSIX 安装期间/之后运行脚本?