Skip to content

Instantly share code, notes, and snippets.

@adbertram
Last active June 17, 2021 17:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save adbertram/da5162cc22c796170d2624e7acaf5fab to your computer and use it in GitHub Desktop.
Save adbertram/da5162cc22c796170d2624e7acaf5fab to your computer and use it in GitHub Desktop.
function Set-RegistryValueForAllUsers {
<#
.SYNOPSIS
This function uses Active Setup to create a "seeder" key which creates or modifies a user-based registry value
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.
.EXAMPLE
PS> Set-RegistryValueForAllUsers -RegistryInstance @{'Name' = 'Setting'; 'Type' = 'String'; 'Value' = 'someval'; 'Path' = 'SOFTWARE\Microsoft\Windows\Something'}
This example would modify the string registry value 'Type' in the path 'SOFTWARE\Microsoft\Windows\Something' to 'someval'
for every user registry hive.
.PARAMETER RegistryInstance
A hash table containing key names of 'Name' designating the registry value name, 'Type' to designate the type
of registry value which can be 'String,Binary,Dword,ExpandString or MultiString', 'Value' which is the value itself of the
registry value and 'Path' designating the parent registry key the registry value is in.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[hashtable[]]$RegistryInstance
)
try {
New-PSDrive -Name HKU -PSProvider Registry -Root Registry::HKEY_USERS | Out-Null
## Change the registry values for the currently logged on user. Each logged on user SID is under HKEY_USERS
$LoggedOnSids = (Get-ChildItem HKU: | where { $_.Name -match 'S-\d-\d+-(\d+-){1,14}\d+}).PSChildName
Write-Verbose "Found $($LoggedOnSids.Count) logged on user SIDs"
foreach ($sid in $LoggedOnSids) {
Write-Verbose -Message "Loading the user registry hive for the logged on SID $sid"
foreach ($instance in $RegistryInstance) {
## Create the key path if it doesn't exist
New-Item -Path "HKU:\$sid\$($instance.Path | Split-Path -Parent)" -Name ($instance.Path | Split-Path -Leaf) -Force | Out-Null
## Create (or modify) the value specified in the param
Set-ItemProperty -Path "HKU:\$sid\$($instance.Path)" -Name $instance.Name -Value $instance.Value -Type $instance.Type -Force
}
}
## Create the Active Setup registry key so that the reg add cmd will get ran for each user
## logging into the machine.
## http://www.itninja.com/blog/view/an-active-setup-primer
Write-Verbose "Setting Active Setup registry value to apply to all other users"
foreach ($instance in $RegistryInstance) {
## Generate a unique value (usually a GUID) to use for Active Setup
$Guid = [guid]::NewGuid().Guid
$ActiveSetupRegParentPath = 'HKLM:\Software\Microsoft\Active Setup\Installed Components'
## Create the GUID registry key under the Active Setup key
New-Item -Path $ActiveSetupRegParentPath -Name $Guid -Force | Out-Null
$ActiveSetupRegPath = "HKLM:\Software\Microsoft\Active Setup\Installed Components\$Guid"
Write-Verbose "Using registry path '$ActiveSetupRegPath'"
## Convert the registry value type to one that reg.exe can understand. This will be the
## type of value that's created for the value we want to set for all users
switch ($instance.Type) {
'String' {
$RegValueType = 'REG_SZ'
}
'Dword' {
$RegValueType = 'REG_DWORD'
}
'Binary' {
$RegValueType = 'REG_BINARY'
}
'ExpandString' {
$RegValueType = 'REG_EXPAND_SZ'
}
'MultiString' {
$RegValueType = 'REG_MULTI_SZ'
}
default {
throw "Registry type '$($instance.Type)' not recognized"
}
}
## Build the registry value to use for Active Setup which is the command to create the registry value in all user hives
$ActiveSetupValue = "reg add <code>"{0}</code>" /v {1} /t {2} /d {3} /f" -f "HKCU\$($instance.Path)", $instance.Name, $RegValueType, $instance.Value
Write-Verbose -Message "Active setup value is '$ActiveSetupValue'"
## Create the necessary Active Setup registry values
Set-ItemProperty -Path $ActiveSetupRegPath -Name '(Default)' -Value 'Active Setup Test' -Force
Set-ItemProperty -Path $ActiveSetupRegPath -Name 'Version' -Value '1' -Force
Set-ItemProperty -Path $ActiveSetupRegPath -Name 'StubPath' -Value $ActiveSetupValue -Force
}
} catch {
Write-Warning -Message $_.Exception.Message
}
}
@AndreasRogge
Copy link

Hi,
from line 25 to 28 you pasted a link to MS Gallery. This has to be removed inside the script ;).

Best regards

@andyval
Copy link

andyval commented Jun 17, 2021

Hi,
Line 31 will overwrite the registry key, even if it does exist. This can be problematic if there are other properties already in that key that aren't needing changes. I made the change on my fork here: https://gist.github.com/andyval/098951d277be90d9fb10b154b8de22ad . It wouldn't allow me to do a pull request.

Also, I added a parameter to be able to name the Active Setup Name instead of defaulting all of them to "Active Setup Test" which would allow me to know the purpose of the regkey.

Great script though, thanks for this! I used it to Disable news and interests on the taskbar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment