No announcement yet.

Detect user UI Display Language from system account

  • Filter
  • Time
  • Show
Clear All
new posts

  • Detect user UI Display Language from system account


    I need a script to detect if the users display language is DE or EN.
    The reg key that holds this value is:
    HKEY_CURRENT_USER\Control Panel\Desktop\PrefferedUILanguage with the value de-DE or en-US.

    The problem is that if i check this location from a system account there is no PrefferedUILanguage string because the string is a user specific setting.
    So i have to check in all the SIDS under HKEY_USERS for the PrefferedUILanguage string, and i don't know how to do that.

    Can someone please help me out.

    Thank you.

  • #2
    Re: Detect user UI Display Language from system account

    The SIDS listed under HKEY_USERS are only the sids for users with unclosed sessions.

    However it is possible to load also other's hives:
    reg.exe load "HKLM\LoadUserHive" "C:\Documents and Settings\[name]\NTUSER.DAT"

    Don't forget to unload them again when you're done:
    reg.exe unload "HKLM\LoadUserHive"

    Load the hives under HKLM (my choice) or under HKU.

    @echo off
    pushD "%userprofile%\.." ||goto:noloading
    >"c:\PreferredUILanguages.log" 2>&1 call:FindHives
    goto:EOF + + + Sub Routines + + +
    for /f "tokens=*" %%* in ('dir . /AD /B') do (
       If exist "%CD%\%%*\NTUSER.DAT" (
       call:RegCmds "%TempKey% %%*" "%CD%\%%*\NTUSER.DAT"))
    exit /b 0
    echo.--- %date% %time% ---
    reg.exe LOAD %*
    rem reg.exe ADD "%~1\Control Panel\Desktop" /v PreferredUILanguagesPending /t REG_MULTI_SZ /d "en-US" /F
    reg.exe QUERY "%~1\Control Panel\Desktop" |findstr /ric:"PreferredUILanguages\>"
    reg.exe QUERY "%~1\Control Panel\Desktop" |findstr /ric:"PreferredUILanguagesPending\>"
    reg.exe UNLOAD %1
    exit /b 0

    Or, instead of using a one-time admin script, you can create a user logon script (GPO).
    It'll query the registry for the values during every login.
    The script can change the PreferredUILanguagesPending (!!!) value as desired.
    If the value PreferredUILanguagesPending is not equal to the value of PreferredUILanguages it then forces the user session to log off. The next logon the UI-Language will be changed. Like the sample batch above, you can also just query the current values and output to a log file.

    Last edited by Rems; 25th September 2012, 19:38.

    This posting is provided "AS IS" with no warranties, and confers no rights.


    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts


    • #3
      Re: Detect user UI Display Language from system account


      Thank you for the reply, but i don't think i can use this solution.

      I need to check the value of the following reg key on W7 OS:

      HKCU\Control Panel\Desktop\PreferredUILanguages.
      This value exist only if a language pack is installed and is de-DE if the DE lp is installed. I need to do this to determine what the Display Language is set to the respective user.

      However i cannot search this value from a script ran with system or admin rights. Because it will search in the HKCU of the system or admin account.

      So i need to check in the HKU hives.

      I have managed to come up with this script:

      Const HKEY_USERS = &H80000003
      Const HKEY_LOCAL_MACHINE = &H80000002
      Const OverwriteExisting = True
      Dim strComputer, oReg, oWMISvc, regEx, objReg, fso, sh, StrKeyPath1, objFSO, LANG, strKeyPath, arrSubKeys, subkey, arrValues, strValue, sUsr
      strComputer = "."  'for local computer enter "."
      	Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
          	strComputer & "\root\default:StdRegProv")
      	Set oWMISvc = GetObject("winmgmts:\root\cimv2")
      	Set regEx = New RegExp
      	Set FSO = CreateObject("Scripting.FileSystemObject")
        	set sh = createObject("")
      	Set objFSO = CreateObject("Scripting.FileSystemObject")
      	Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
         regEx.Pattern = "^S-1-5-21-[0-9]*-[0-9]*-[0-9]*-[0-9]*$"
         regEx.IgnoreCase = TRUE
         strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
         oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
          For Each subkey In arrSubKeys
      		If regEx.Test(subkey)=TRUE Then
      			sUsr = resolveSID(subkey)
      			strKeyPath1 = subkey & "\Control Panel\Desktop"
      			objReg.GetMultiStringValue HKEY_USERS, strKeyPath1, "PreferredUILanguages", arrValues
      			If IsNull(arrValues) Then
      				LANG = 0
      				For Each strValue In arrValues
      					LANG = strValue
      			End If
      		End If
      If LANG = "de-DE" then
      	sh.RegWrite "HKLM\Software\rwt GmbH\UILanguage", "DE", "REG_SZ"
      	sh.RegWrite "HKLM\Software\rwt GmbH\UILanguage", "nonDE", "REG_SZ"
      End If
      Function resolveSID(sid)
         Dim strUser
         On Error Resume Next
         With oWMISvc
      		 With .Get("Win32_SID.SID='" & sid & "'")
      		   strUser = .AccountName
      		 End With
      	 End With
         If len(strUser) = 0 Then
           resolveSID = Empty
           resolveSID = strUser
         End If
      End function
      It basically checks all the hives under HKU and if it finds the PreferredUILanguages value it stores it in a HKLM key with the vale DE for de-DE UI language, and nonDE for anything else.

      It has only one flaw:

      If I set the admin UI lang to DE and then log to a user account and leave the UI to english, then it doesn't work. Because when i open the cmd as admin i load the admin hive under HKU. The admin hive has the PreferredUILanguages value set to de-DE (because of the UI lang change i did on admin). The user on which i am logged is set to en-US. However the script returns the value de-DE because it finds the value under the admin hive.

      I have to detect somehow which user is logged on. And i have to do this from a vbs script which is ran with system rights.

      I'll try the load hive thing and i'll hope i can manage to make it work somehow.


      • #4
        Re: Detect user UI Display Language from system account

        Originally posted by KodiaK View Post
        It basically checks all the hives under HKU
        What is the OS of the machine? Is the current user logged on with a console session? Is it a local user or domain user?

        If you run the script on a computer as an domain admin - then it should do something like:
        ' Run as an administrator! (a domain admin  if the current user is a domain user)
        Const HKU  = &H80000003
        Const HKLM = &H80000002
        rem On Error Resume Next
        strComputer = "."    ' or enter the name of a remote computer
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
        For Each objItem in colItems
           sUser = split(objItem.UserName,"\")(1)
           sDomain = split(objItem.UserName,"\")(0)
           exit for
        Set objAccount = objWMIService.Get _
            ("Win32_UserAccount.Name='" & sUser & "',Domain='" & sDomain & "'")
        strSID  = objAccount.SID
        SubKey1 = strSID & "\Control Panel\Desktop"
        SubKey2 = "Software\rwt GmbH"
        Lang = "nonDE"
        Set regEx = New RegExp
        regEx.Pattern = "^S-1-5-21-[0-9]*-[0-9]*-[0-9]*-[0-9]*$"
        regEx.IgnoreCase = TRUE
        If (regEx.Test(strSID) = False) Then wscript.quit
        Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
           strComputer & "\root\default:StdRegProv")
        oReg.CreateKey HKLM, SubKey2
        oReg.GetMultiStringValue HKU, SubKey1, _
               "PreferredUILanguages", arrValues
        If NOT IsNull(arrValues) Then
           If instr(1,join(arrValues), "DE",1) Then Lang = "DE"
        End If
        oReg.SetStringValue HKLM, SubKey2, "UILanguage", Lang
        Maybe you can run the script as scheduled tasks that runs after user login.

        A totally different solution could be that you (or a startup script or a GPP) change the permissions on the 'rwt GmbH' subkey to allow users to edit the values.

        Last edited by Rems; 27th September 2012, 10:34.

        This posting is provided "AS IS" with no warranties, and confers no rights.


        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts


        • #5
          Re: Detect user UI Display Language from system account

          Thanks a million.
          It works like a charm.
          I see you have simplified my script very much
          I am very unskilled at scripting, and i did that script pretty much copy/paste style.
          I have tested it from psexec on a user account (both user and admin are local accounts)

          The end package will be installed via SCCM, but i suppose the script will work from SCCM too, right?

          I cannot use it at logon because i have to install an application in a different folder if the Display Language is set to Deutsch. So i use the script to detect the UI Language and based on that (the reg key set in HKLM) the msi package will install to a different folder if the UI is set to DE.