pf-WinRegistry.ps1
function Update-RegistryKey ($path, $name, $value) { if ( -not (test-path $path ) ) { $parent = Split-Path $path -Parent Update-RegistryKey -path $parent New-Item -Path $path -ItemType Directory | Out-Null } if ($name) { Set-ItemProperty -path $path -Name $name -Value $value } } function Update-RegistryKey:::Example { $regKey="HKLM:\SOFTWARE\Microsoft\PSModules\Update-RegistryKeyTest" Remove-Item $regKey -Force -Recurse -ErrorAction SilentlyContinue Update-RegistryKey -path $regKey -name AnyName -value 1234 Get-RegistryKey -path "$regKey" -Name AnyName | Assert -eq 1234 } function Update-RegistryKey_Scope($path, $name, $value, [ScriptBlock]$action) { try { $oldValue = Get-RegistryKey -path $path -Name $name Update-RegistryKey -path $path -name $name -value $value . $action } finally { if ($null -eq $oldValue) { remove-itemproperty -Path $path -Name $name -Force # Remove-Item $path -Force -Recurse -ErrorAction SilentlyContinue } else { Update-RegistryKey -path $path -name $name -value $oldValue } } } function Update-RegistryKey_Scope:::Example { $regKey="HKLM:\SOFTWARE\Microsoft\PSModules\Update-RegistryKey_ScopeTest" Remove-Item $regKey -Force -Recurse -ErrorAction SilentlyContinue Update-RegistryKey_Scope -path $regKey -name AnyName -value 1234 -action { Get-RegistryKey -path "$regKey" -Name AnyName | Assert -eq 1234 } $result = Get-RegistryKey -path "$regKey" -Name AnyName $result | Assert -eq $null } function Get-RegistryKeyTable{ [CmdletBinding()] Param( [Parameter(ValueFromPipeline=$true)] $path, $Name = '*', [Switch]$Recursive ) process { $path = Get-path -path $path $regPath = $path | Update-Prefix -prefix 'HKEY_CURRENT_USER\' -replace 'HKCU:\' $regPath = $path | Update-Prefix -prefix 'HKEY_LOCAL_MACHINE\' -replace 'HKLM:\' #For registry keys we seem to need to add a prefix to ensure that they can be correctly found if($path -is [Microsoft.Win32.RegistryKey]){ $regPath = "Registry::$regPath" } if ( -not (Test-Path $regPath ) ) { return } $props = Get-ItemProperty -path $regPath if ( $props ) { $regProps = $props | Get-Member -Name $Name -MemberType 'NoteProperty' | Sort-Object -Property Name | Where-Object { $_.Definition -like 'int *' -or $_.Definition -like 'string *' } $result = $regProps.Name | ForEach-Object { $value = $props.$_ [PsCustomObject]@{ Path = $path; Name = $_ ; Value = $value } } $result } if ($Recursive ) { Get-ChildItem -Path $regPath | Get-RegistryKeyTable -Name $Name -Recursive } } } function Get-RegistryKeyTable:::Example { $proxyRegPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' Get-RegistryKeyTable -path $proxyRegPath -Name * -Recursive } function Get-RegistryKey{ [CmdletBinding()] Param( [Parameter(ValueFromPipeline=$true)] $path, $Name ) process { $result = Get-RegistryKeyTable -path $path -Name $Name $result.Value } } function Get-RegistryKey:::Example { $proxyRegPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' Get-RegistryKey -path $proxyRegPath -Name ProxyOverride } function Export-RegistryKey:::Example { $regPath = @( 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' 'HKCU:\Software\Microsoft\Internet Explorer' ) $result = $proxyRegPath | Get-RegistryKeyTable -Name * -Recursive $result | ConvertTo-Csv | Set-Content -Path "$env:temp\regExport_A.csv" Get-Content -Path "$env:temp\regExport.csv" | ConvertFrom-Csv } |