|
| 1 | +function Set-RegistryValueForAllUsers { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + This function uses Active Setup to create a "seeder" key which creates or modifies a user-based registry value |
| 5 | + for all users on a computer. If the key path doesn't exist to the value, it will automatically create the key and add the value. |
| 6 | + |
| 7 | + Thanks to Ben Reader (@powers_hell) for improving upon this. |
| 8 | + .EXAMPLE |
| 9 | + PS> Set-RegistryValueForAllUsers -RegistryInstance @{'Name' = 'Setting'; 'Type' = 'String'; 'Value' = 'someval'; 'Path' = 'SOFTWARE\Microsoft\Windows\Something'} |
| 10 | + This example would modify the string registry value 'Type' in the path 'SOFTWARE\Microsoft\Windows\Something' to 'someval' |
| 11 | + for every user registry hive. |
| 12 | + .PARAMETER RegistryInstance |
| 13 | + A hash table containing key names of 'Name' designating the registry value name, 'Type' to designate the type |
| 14 | + of registry value which can be 'String,Binary,Dword,ExpandString or MultiString', 'Value' which is the value itself of the |
| 15 | + registry value and 'Path' designating the parent registry key the registry value is in. |
| 16 | + #> |
| 17 | + [CmdletBinding()] |
| 18 | + param ( |
| 19 | + [Parameter(Mandatory = $true)] |
| 20 | + [hashtable[]]$RegistryInstance |
| 21 | + ) |
| 22 | + try { |
| 23 | + New-PSDrive -Name HKU -PSProvider Registry -Root Registry::HKEY_USERS | Out-Null |
| 24 | + |
| 25 | + ## Change the registry values for the currently logged on user. Each logged on user SID is under HKEY_USERS |
| 26 | + $LoggedOnSids = $(Get-ChildItem HKU: | Where-Object { $_.Name -match 'S-\d-\d+-(\d+-){1,14}\d+$' } | foreach-object { $_.Name }) |
| 27 | + Write-Verbose "Found $($LoggedOnSids.Count) logged on user SIDs" |
| 28 | + foreach ($sid in $LoggedOnSids) { |
| 29 | + Write-Verbose -Message "Loading the user registry hive for the logged on SID $sid" |
| 30 | + foreach ($instance in $RegistryInstance) { |
| 31 | + ## Create the key path if it doesn't exist |
| 32 | + if (!(Test-Path "HKU:\$sid\$($instance.Path)")) { |
| 33 | + New-Item -Path "HKU:\$sid\$($instance.Path | Split-Path -Parent)" -Name ($instance.Path | Split-Path -Leaf) -Force | Out-Null |
| 34 | + } |
| 35 | + ## Create (or modify) the value specified in the param |
| 36 | + Set-ItemProperty -Path "HKU:\$sid\$($instance.Path)" -Name $instance.Name -Value $instance.Value -Type $instance.Type -Force |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + ## Create the Active Setup registry key so that the reg add cmd will get ran for each user |
| 41 | + ## logging into the machine. |
| 42 | + ## http://www.itninja.com/blog/view/an-active-setup-primer |
| 43 | + Write-Verbose "Setting Active Setup registry value to apply to all other users" |
| 44 | + foreach ($instance in $RegistryInstance) { |
| 45 | + ## Generate a unique value (usually a GUID) to use for Active Setup |
| 46 | + $Guid = [guid]::NewGuid().Guid |
| 47 | + $ActiveSetupRegParentPath = 'HKLM:\Software\Microsoft\Active Setup\Installed Components' |
| 48 | + ## Create the GUID registry key under the Active Setup key |
| 49 | + New-Item -Path $ActiveSetupRegParentPath -Name $Guid -Force | Out-Null |
| 50 | + $ActiveSetupRegPath = "HKLM:\Software\Microsoft\Active Setup\Installed Components\$Guid" |
| 51 | + Write-Verbose "Using registry path '$ActiveSetupRegPath'" |
| 52 | + |
| 53 | + ## Convert the registry value type to one that reg.exe can understand. This will be the |
| 54 | + ## type of value that's created for the value we want to set for all users |
| 55 | + switch ($instance.Type) { |
| 56 | + 'String' { |
| 57 | + $RegValueType = 'REG_SZ' |
| 58 | + } |
| 59 | + 'Dword' { |
| 60 | + $RegValueType = 'REG_DWORD' |
| 61 | + } |
| 62 | + 'Binary' { |
| 63 | + $RegValueType = 'REG_BINARY' |
| 64 | + } |
| 65 | + 'ExpandString' { |
| 66 | + $RegValueType = 'REG_EXPAND_SZ' |
| 67 | + } |
| 68 | + 'MultiString' { |
| 69 | + $RegValueType = 'REG_MULTI_SZ' |
| 70 | + } |
| 71 | + default { |
| 72 | + throw "Registry type '$($instance.Type)' not recognized" |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + ## Build the registry value to use for Active Setup which is the command to create the registry value in all user hives |
| 77 | + $ActiveSetupValue = "reg add `"{0}`" /v {1} /t {2} /d {3} /f" -f "HKCU\$($instance.Path)", $instance.Name, $RegValueType, $instance.Value |
| 78 | + Write-Verbose -Message "Active setup value is '$ActiveSetupValue'" |
| 79 | + ## Create the necessary Active Setup registry values |
| 80 | + Set-ItemProperty -Path $ActiveSetupRegPath -Name '(Default)' -Value 'Active Setup Test' -Force |
| 81 | + Set-ItemProperty -Path $ActiveSetupRegPath -Name 'Version' -Value '1' -Force |
| 82 | + Set-ItemProperty -Path $ActiveSetupRegPath -Name 'StubPath' -Value $ActiveSetupValue -Force |
| 83 | + } |
| 84 | + } |
| 85 | + catch { |
| 86 | + Write-Warning -Message $_.Exception.Message |
| 87 | + } |
| 88 | +} |
0 commit comments