Set up HKU value for logged on user

I’m trying to do a Set-RegistryKey for the logged on user, but i dont get it run with the invoke command
when i put the following lines in the script i get an error during the installation that SID can not be NULL (or similar message):

Set-RegistryKey -Key ‘HKCU:SOFTWARE\Software\xxx’ -Name ‘xxx’ -Value ‘xxx’ -Type String -SID $UserProfile.SID

Invoke-HKCURegistrySettingsForAllUsers -RegistrySettings $HKCURegistrySettings

The other mentioned way is always set-activesetup, but this will not set the HKU value for the already create user, or?

Can anyone give me examples how i can set-up registry values for logged on user?

You need to define the $HKCURegistrySettings script block with your HKCU operations before passing it to Invoke-HKCURegistrySettingsForAllUsers, $UserProfile.SID would only be available in that scriptblock hence the ‘cannot be NULL’ exception, here’s how I’ve used it in the past:

[scriptblock]$HKCURegistrySettings = {            
Set-RegistryKey -Key 'HKCU:\Software\Microsoft\Some Key' -Name 'Some Name' -Value 1 -Type DWord -SID $UserProfile.SID
}
Invoke-HKCURegistrySettingsForAllUsers -RegistrySettings $HKCURegistrySettings

This will effectively add the key to all HKCU hives present on the system.

Alternatively, you could use the $CurrentConsoleUserSession.SID outside of the scriptblock if you only need to touch the current user.

4 Likes

What Johann said.
Also thanks for the tip to get logged in user’s SID

1 Like

thanks for your reply, i will give it a try !