Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


November 21, 2005

How can I update all profiles on a machine even if they're not currently loaded?

RSS
Subscribe to Windows IT Pro | See More Active Directory (AD) Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

A. In the FAQ "How can I update a registry value for all users on a machine?" at http://www.windowsitpro.com/Article/ArticleID/48128/48128.html , I explain how to enumerate HKEY_USERS and update each profile with a registry change; however, that change works only for profiles that are loaded. For example, if I have 100 profiles stored on a machine, they're not all loaded all the time under HKEY_USERS. Therefore, my original code would have updated only currently logged-on users or profiles that had processes still running under them or the well-known built-in credentials (NT System, Local Service, and Network Service) that are always loaded. You should test this code to ensure that it behaves as expected, especially if you run it when a user might log on because it could cause a new profile to be created for the user. I typically use this method as part of an OS upgrade scenario when I want to set something initially or outside of Group Policy for the existing profiles on a machine before user logon is possible.

I've created a script that takes a different approach to updating profiles. The code, which you can download at http://www.windowsitpro.com/articles/download/updateprof.zip enumerates the "Documents and Settings" folder and, for each subfolder (a profile), mounts the ntuser.dat file, modifies the file, unloads it, then moves to the next subfolder. The code also changes the desktop wallpaper, but you can obviously change the actual "work" that the script performs.


Dim objFSO, oShell, strBasePath, objFolder, objSubFolders, objSubFolder, strRun, nRtn
Dim strKeyPath, strKeyPathUser
const HKEY_USERS = &H80000003

Set objFSO = CreateObject("Scripting.FileSystemObject")

strKeyPath = "\Control Panel\PowerCfg\GlobalPowerPolicy" 

Set oShell = WScript.CreateObject("WScript.Shell") 
strBasePath = oShell.ExpandEnvironmentStrings("%ALLUSERSPROFILE%") 
strBasePath = left(strBasePath,InStrRev(strBasePath,"\")-1)
wscript.echo strBasePath

Dim binValue() 
strComputer = "." 

Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv") 


Set objFolder = objFSO.GetFolder(strBasePath)
Set objSubFolders = objFolder.SubFolders
For Each objSubFolder in objSubFolders
    Wscript.Echo objSubFolder.Name
    If objFso.FileExists(strBasePath & "\" & objSubFolder.Name & "\" & "ntuser.dat") = True Then
        strRun = "REG.EXE load " & "HKU\TempHive " & Chr(34) & strBasePath & "\" & objSubFolder.Name & "\" & "ntuser.dat" & Chr(34)
        nRtn = oShell.Run(strRun, 1, True) 

        ' The work portion. Change this to anything task you want to perform.
        if objReg.GetBinaryValue(HKEY_USERS, "TempHive" & strKeyPath, "Policies", binValue) = 0 then 
            objReg.GetStringValue HKEY_USERS, "TempHive\Software\Microsoft\Windows\CurrentVersion\Explorer", "Logon User Name", userName 
            Wscript.Echo "Updating username " & userName 
            binValue(51)=128 
            objReg.SetBinaryValue HKEY_USERS, "TempHive" & strKeyPath, "Policies", binValue 
            objReg.SetStringValue HKEY_USERS, "TempHive\Control Panel\Desktop" , "Wallpaper", "D:\Multimedia\Images\Space\rocks.bmp"
            objReg.SetStringValue HKEY_USERS, "TempHive\Control Panel\Desktop" , "WallpaperStyle", "2"
        end if 
        ' End of the work portion

        strRun = "REG.EXE unload HKU\TempHive"
        nRtn = oShell.Run(strRun, 1, True) 
    End If
Next
You should run the script under the system context so that it has write access to everyone's profile (e.g., run it as a scheduled job). Also, instead of enumerating "Documents and Settings," you can enumerate HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList and read the list of profiles.

Thanks to Toby Ovod-Everett for pointing out the limitation in my previous FAQ.

End of Article



Reader Comments

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...


Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Meeting Compliance Objectives in SharePoint

Email Controls and Regulatory Compliance

Related Events Troubleshooting Active Directory

Deep Dive into Windows Server 2008 R2 presented by John Savill

Concrete Ways to Make Sure Your SharePoint Deployment Doesn't Blow Up

Check out our list of Free Email Newsletters!

Active Directory (AD) eBooks The Essentials Series: Active Directory 2008 Operations

Keeping Your Business Safe from Attack: Monitoring and Managing Your Network Security

Windows 2003: Active Directory Administration Essentials

Related Active Directory (AD) Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement