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.