Add a key to HKEY_CURRENT_USER for all users

97,711

Solution 1

You'd have to go through all the different users under HKEY_USERS, which requires elevated rights. And doesn't capture any users that have not yet been created. That's just the wrong approach.

The way to do it is to add the default values to a corresponding key under HKLM at install time. When your program attempts to read from the registry, it looks in HKCU first, and if your key is not present, it copies the information from the corresponding key in HKLM to the key in HKCU.

A general rule of installer programs is that they should not rely on being run by the user that will subsequently use the program that has been installed. Certainly in corporate settings programs are usually installed under a user account that will never subsequently run the program being installed.

Solution 2

In some cases, Active Setup may be the solution.

It works by adding a key to HKLM\SOFTWARE\Microsoft\Active Setup\Installed Components\%package name% with a version number. When a user logs in Windows checks this location and compares it to HKCU\SOFTWARE\Microsoft\Active Setup\Installed Components\%package name%. If it is missing or a lower version then it runs whatever has been set in HKLM\SOFTWARE\Microsoft\Active Setup\Installed Components\%package name%\StubPath.

You can do some custom things this way, for example I used it to add a certain script (to map a network drive) to the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run the following way:

reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\MapDrive" /v "Version" /d "1" /t REG_SZ /f

reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\MapDrive" /v "StubPath" /d "reg add HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run /v "MountDrive" /d "C:\map.cmd" /t REG_DWORD /f" /f

What happens here:

  • when the user next logs on, Active Setup checks if there is a string Version with value of 1 or greater in the registry under key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\MapDrive. There is none, therefore it creates it, and also runs the second reg add command, which adds a string with a value C:\map.cmd under HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
  • it only happens once, because on each consecutive log on Active Setup will find out that the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\MapDrive now has a Version under it.

Sounds complicated, but makes sense once you figure it out, and very useful.

http://wpkg.org/Adding_Registry_Settings#Adding_entries_to_HKCU_for_all_users

Solution 3

This is my process as I currently work in Windows 7

  • Find the key/s you want under HKCU in regedit, export it/them.

  • Now right click on the HKEY_USERS key and select load hive. Browse to C:\Users\Default and select NTUSER.DAT. Name the hive whatever you like (eg NAMEHERE).

  • in your exported reg file replace all instances of [HKEY_CURRENT_USER\ with [HKEY_USERS\NAMEHERE\ (or whatever you named your hive) and save the file

  • double click the reg file to merge it

  • Highlight the NAMEHERE key and select unload hive.

Now for any new profiles they get those keys. Existing profiles don't though so you won't see it work if you log in with a pre-existing profile. If you want to delete the profile I find the cleanest way is right click computer, select properties, advanced system settings and then select settings under user profiles. Highlight the profile you want to delete and select delete.

It seems this doesn't work all the time. I'm here looking for information about why this isn't working for the keys I'm trying to add under HKEY_CURRENT_USER\Software\Classes\ but until now this process has always worked. I'll add a note if I see why that isn't working - I'm currently wondering if I have to add it through policy in the user context.

Share:
97,711
David Brunelle
Author by

David Brunelle

A programmer, not much more to know about me :)

Updated on August 10, 2021

Comments

  • David Brunelle
    David Brunelle over 2 years

    I have an installer which installs a key on the HKEY_CURRENT_USER. When I run the installer, it only adds it on the user that is installing. Is there a way to add the key to all users at once?

    Thanks

  • David Brunelle
    David Brunelle about 11 years
    Noted. In my case, I did find a key that is "equivalent" in the local machine so that I could do what I needed to, but I still wondered if there was a way.
  • Paul
    Paul over 10 years
    "The way to do it is ..." - that works, but what is the correct way to uninstall an app with this approach?
  • Daniel Lindegaard
    Daniel Lindegaard over 5 years
    Not a part of the question asked by the OP, so this information have no relevance.
  • Daniel Lindegaard
    Daniel Lindegaard over 5 years
    As stated above by @Evgeny you can at least use Active Setup, so yes you can. But there is ways to write to a default user setting, and what that default user have, will be dublecated to all new users. Allso stated above.