The Sysadmins

Tips and tricks from the Sysadmins

Create Environment Variables From Active Directory

I needed to create a firstname.lastname variable to automate an Outlook profile update via PRF. By default Windows has the %username% variable, which in my instance returns tuser- I need to set-up a new Outlook Profile for an account in a different forest which uses test.user, this script allows me to deploy the custom environment variable and therefore the PRF for the target mailbox.

I’ve given a few additional examples in the script, if you want to deploy via group policy add it as a user logon script.

The VBS Script

Set objShell = CreateObject("WScript.Shell")
Set objUserEnv = objShell.Environment("USER")
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
' This will create the variable %first.last% for the first.lastname
objUserEnv("first.last") = objUser.givenName & "." & objUser.sn
' This will create the variable %first% for the first name
objUserEnv("first") = objUser.givenName
' This will create the variable %last% for the surname
objUserEnv("last") = objUser.sn
' This will create the variable %mail% for the primary mail address
objUserEnv("cn") = objUser.mail

The variable is persistent, if you find that it doesn’t work for you, and you already have the command prompt open, you will need to close and reopen the command prompt for it to take effect.

AD VBS Variable

4 Comments

  1. IT Guy lost in space

    June 16, 2014 at 7:42 pm

    This is a brilliant approach to PRF file customization. I owe you a beer and thanks for the info! btw, i think you meant for the last line to be objUserEnv(“mail”) instead of cn?

  2. IT Guy lost in space

    June 26, 2014 at 3:29 pm

    workaround I used to make variables accessible within the same script the PRF is called from below. i gave my variables names like “prf-” to distinguish a bit more –

    for /f “tokens=2*” %%A in (‘REG QUERY “HKEY_CURRENT_USER\environment” /v prf-email’) DO (
    for %%F in (%%B) do (
    set PRF-EMAIL=%%F
    goto :break1
    )
    )
    :break1
    for /f “tokens=2*” %%A in (‘REG QUERY “HKEY_CURRENT_USER\environment” /v prf-displayname’) DO (
    for %%F in (%%B) do (
    set PRF-DISPLAYNAME=%%F
    goto :break2
    )
    )
    :break2

    to permanently clear/reset the variables:

    reg delete HKCU\environment /v first /f
    reg delete HKCU\environment /v last /f
    reg delete HKCU\environment /v first.last /f
    reg delete HKCU\environment /v email /f
    reg delete HKCU\environment /v displayname /f
    reg delete HKCU\environment /v prf-first /f
    reg delete HKCU\environment /v prf-last /f
    reg delete HKCU\environment /v prf-first.last /f
    reg delete HKCU\environment /v prf-email /f
    reg delete HKCU\environment /v prf-displayname /f

  3. Hi is there any chance if i can see the PRF file that you use? thanks

    • Tom@thesysadmins.co.uk

      May 26, 2015 at 8:43 pm

      Hi Eriock

      I’m afraid I don’t have the PRF to hand, this was some time ago!

      Tom

Leave a Reply

Your email address will not be published.

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.