We help IT Professionals succeed at work.

Rebuilding HKEY_USERS hives manually at bootup

Last Modified: 2008-04-24
I'm thinking that in order to do what it is I need to do though, I'm not going to be able to avoid scripting.  Here's the detail.  I'm using DeepFreeze to restore the system configuration after every reboot.  The Docs & Settings folder are located on a non protected partition.  What I need is for the HKEY_USERS rootkey to survive though.  My thoughts are to have a script run (maybe scheduled or just during system shutdown) that will backup all hives in HKEY_USERS, then during system startup, have each of those hives loaded again.  The other method I'm thinking about is to just have a script recurse through the Docs & Settings folder loading every NTUSER.DAT file it finds, but that might be more difficult since we need to properly name the hive.  The reason I like that idea, is it allows me to have another process (DELPROF) clean the profiles periodically so that we're only loading current profiles.

I'm not experienced in scripting though, can anyone point me to a site with some samples that will help me?
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
Here is a script i wrote to change reg entries for all local users of a machine (obviously each ntuser.dat had to be loaded in sequence).

It doesn't do exactly what you need, but there should be a few bits that you can use in your script... I dont have time to strip out the bits you'd need, or explain it properly as im going home now, but ill be back tomorrow to answer any questions if your still not sorted):

If WScript.Arguments.count <> 2 Then WScript.Quit

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Dim objWMIService, UserAccount, myComputer, UserSID, CurrentUser
Dim oShell, RegKey, RegVal

RegVal = WScript.Arguments.Item(1)

Set oShell = CreateObject("Wscript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set myComputer = objWMIService.ExecQuery("SELECT * FROM Win32_UserAccount", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
  For Each UserAccount In myComputer
   Select Case UserAccount.Disabled
        Case True
        Case False
              Select Case UserAccount.Name
                  Case "Administrator"
                  Case oShell.ExpandEnvironmentStrings("%USERNAME%")
                        RegKey = "HKEY_CURRENT_USER\" & WScript.Arguments.Item(0)
                        DoRegChange RegKey, RegVal
                  Case Else
                        If LoadHive(UserAccount.name) = True Then
                              RegKey = "HKEY_USERS\" & UserAccount.name & "\" & WScript.Arguments.Item(0)
                              DoRegChange RegKey, RegVal
                              UnloadHive UserAccount.name
                        End If
            End Select
   End Select
Sub DoRegChange(RegKey, RegVal)
      WScript.Sleep 1000
      oShell.RegWrite RegKey, RegVal
End Sub

Function LoadHive(strUsername)
      Set oFSO = CreateObject("Scripting.FileSystemObject")
      If oFSO.FileExists("C:\Documents And Settings\" & strUsername & "\NTUser.Dat") Then
            oShell.Exec "Reg load " & "HKU\" & strUsername & " " & """C:\Documents And Settings\" & strUsername & "\NTUser.Dat"""
            LoadHive = True
            LoadHive = False
      End If
End Function

Sub UnloadHive(KeyPath)
      oShell.Exec "Reg unload " & "HKU\" & KeyPath
End Sub

Hope this helps,

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.