+ Post New Thread
Results 1 to 6 of 6
Scripts Thread, Script to Echo OU of Current User? in Coding and Web Development; Hi dark-art-of-scripting people. I need a script that'll give me the OU of the user currently logged on to a ...
  1. #1

    Miscbrah's Avatar
    Join Date
    Dec 2011
    Thank Post
    Thanked 266 Times in 163 Posts
    Rep Power

    Script to Echo OU of Current User?

    Hi dark-art-of-scripting people.

    I need a script that'll give me the OU of the user currently logged on to a machine.

    I've currently got:

    Set objSysInfo = Createobject("ADSystemInfo")
    Wscript.Echo "DN of current user: " & objSysInfo.UserName
    Which echos everything from the username name up, but what I really want echoed is just the first "OU=???" above the CN=%username% part.

    Anyone able to sort me? Thanks in advance for reading and thinking about it!

  2. #2

    LosOjos's Avatar
    Join Date
    Dec 2009
    West Midlands
    Thank Post
    Thanked 1,214 Times in 823 Posts
    Rep Power
    Perhaps the Split() function would help?

    VBScript Split Function

    If you want more help with the implementation, please post an example of the result you get (remove any actual names if you want to, just need to see the format of the string you're getting)
    Last edited by LosOjos; 14th March 2013 at 12:26 PM.

  3. Thanks to LosOjos from:

    Miscbrah (14th March 2013)

  4. #3

    Miscbrah's Avatar
    Join Date
    Dec 2011
    Thank Post
    Thanked 266 Times in 163 Posts
    Rep Power
    Oooh look at that! Well that certainly looks like what I'll need in there somewhere. I'll take a little down time (if I get any) and get my head around it.


  5. #4

    Join Date
    Sep 2007
    Thank Post
    Thanked 0 Times in 0 Posts
    Rep Power
    Set objSysInfo = Createobject("ADSystemInfo")
    strName = objSysInfo.UserName
    arrDN = Split(strName, ",")'Split the Users Distinguished Name into an array
    DNCount = Len(arrDN(1))
    'Determines how many characters in the 2nd element of the array which is the where the OU name is (Ex. 'OU=Teachers' would be 11 characters) So DNCount=11
    NewDNCount = DNCount - 3
    ' This takes the DNCount and subtracts 3. Now we know how many characters there are in the string 'OU=Teachers' ( NewDNCount=8 ) minus the 'OU=' part
    Wscript.Echo Right(arrDN(1), NewDNCount)
    'Now we print 'OU=Teachers' except for the 3 characters at the beginning. So we print 'OU=Teachers' from the Right side but only print 8 characters which leaves us with 'Teachers'

    You can also create a variable strOU = Right(arrDN(1), NewDNCount)

    I've also done it this way:
    Set objSysInfo = CreateObject("ADSystemInfo")
    strName = objSysInfo.UserName
    arrUserName = Split(strName, ",") 'Split the User Distinguished Name into array based on commas
    arrOU = Split(arrUserName(1), "=") 'takes the 'OU=Teachers' and splits it into two parts based on the "=" sign
    wscript.echo arrOU(1) 'Echos the 2nd element in the two part array which is 'Techers'

    You can also create a variable from this strOU = arrOU(1)

    Either way works but it's nice to see two ways to accomplish the same task. I do use the 2nd method to map printers to computers based on their OU name and it works great.

  6. #5

    Join Date
    Feb 2007
    51.403651, -0.515458
    Thank Post
    Thanked 2,745 Times in 2,023 Posts
    Rep Power
    Quote Originally Posted by Rich View Post
    Split the User Distinguished Name into array based on commas
    Splitting based on commas is not a good idea.

    Unfortunately, though, thereís a potential problem with that approach, a problem revolving around the fact that itís perfectly permissible to use a comma as part of a userís CN. Suppose our user has this distinguished name:

    CN=Ken Myer, Jr.,OU=Finance,OU=North America,OU=Pacific Coast,dc=fabrikam,dc=com

  7. #6

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Thank Post
    Thanked 1,066 Times in 984 Posts
    Rep Power
    VBScript - Find which OU a user belongs to

    Set objConnection = CreateObject("ADODB.Connection")
    Set objCommand =   CreateObject("ADODB.Command")
    objConnection.Provider = "ADsDSOObject"
    objConnection.Open "Active Directory Provider"
    Set objCommand.ActiveConnection = objConnection
    Set objUser = CreateObject("Wscript.Network")
    FindUser = objUser.UserName    'InputBox("Please Enter A UserName", "Find User OU")
    If FindUser = "" Then
    MsgBox("No UserName Was Added")
    strUser = FindUser
    End If
    objCommand.Properties("Page Size") = 1000
    objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
    objCommand.CommandText = _
        "SELECT distinguishedName FROM 'LDAP://dc=yourdomain,dc=com'WHERE objectCategory='user'AND sAMAccountName='" & strUser & "'"
    Set objRecordSet = objCommand.Execute
    Do Until objRecordSet.EOF
        strDN = objRecordSet.Fields("distinguishedName").Value
        arrPath = Split(strDN, ",")
        intLength = Len(arrPath(1))
        intNameLength = intLength - 3
        Wscript.Echo "User :" & strUser & vbcrlf & "OU is : " & Right(arrPath(1), intNameLength)
    Made the relevant sections bold, you need to adjust the LDAP path according to your domain so dc=my,dc=domain,dc=com or whatever it is

    I have also taken away the InputBox prompt and replaced this with

    Set objUser = CreateObject("Wscript.Network")

    Which returns the currently logged on username correctly, if you use the inputbox and enter the username in wrong then you get an error.

    Original link that I found that had similar code was here :


    Although I could not get it to work as it kept failing on line 10 due to unterminated string etc , didn't have time to look at the code snippet correctly so found the above one which I got working a bit easier.
    Last edited by mac_shinobi; 15th March 2013 at 09:21 AM.

+ Post New Thread

Similar Threads

  1. Replies: 4
    Last Post: 14th October 2011, 02:40 PM
  2. Replies: 36
    Last Post: 5th August 2011, 07:13 AM
  3. Replies: 5
    Last Post: 14th June 2008, 12:18 AM
  4. Replies: 0
    Last Post: 20th August 2007, 02:20 PM
  5. Replies: 12
    Last Post: 12th September 2005, 10:47 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts