PowerShell to set the registry for all users

Just another Tech site

PowerShell to set the registry for all users

Here is a PowerShell script that sets a registry value for all users on a Windows machine. This script modifies the registry under the HKEY_USERS hive, which contains subkeys for each user profile.

# Define the registry path and value
$registryPath = "Software\YourApp\Settings"
$valueName = "YourSetting"
$valueData = "YourValue"

# Get all user SIDs
$userSIDs = Get-ChildItem "HKU:\" | Where-Object { $_.PSChildName -match 'S-\d-\d+-(\d+-){1,14}\d+$' }

foreach ($sid in $userSIDs) {
    try {
        # Construct the full registry path for the current user
        $fullRegistryPath = "HKU\$($sid.PSChildName)\$registryPath"

        # Create the registry key if it doesn't exist
        if (-not (Test-Path $fullRegistryPath)) {
            New-Item -Path $fullRegistryPath -Force | Out-Null
        }

        # Set the registry value
        Set-ItemProperty -Path $fullRegistryPath -Name $valueName -Value $valueData
        Write-Output "Successfully set $valueName for user $($sid.PSChildName)"
    } catch {
        Write-Output "Failed to set $valueName for user $($sid.PSChildName): $_"
    }
}

Explanation:

  1. Registry Path and Value: Define the registry path and the value you want to set.
  2. Get User SIDs: Retrieve all user SIDs from the HKEY_USERS hive.
  3. Loop Through SIDs: Iterate through each SID to set the registry value.
  4. Create Registry Key: Create the registry key if it doesn’t exist.
  5. Set Registry Value: Set the specified registry value for each user.

This script ensures that the registry setting is applied to all user profiles on the machine. If you have any specific requirements or need further customization, feel free to let me know!