I am setting up a Terminal Server 2008 which will be used by different client organisations, each with multiple individual user accounts. I would like each client organisation to have a drive mapped to \server\clients\

Their OU name is also their client name, so I would like to be able to find their current OU and then use it for the mapping command. The OUs are hierarchicals, so it is the bottom-most OU name I need.

Dedicated Clients\AjaxCorp

Should get a drive mapped to

Any suggestions on how I can get the OU? I am sure it must be easy, I just haven't figured it out...

I did find information about how to do this with VB script, but as it is a whole new environment I thought it would be nice to use PowerShell instead.


This will get you the LDAP path to the current computer:

$objDomain = New-Object System.DirectoryServices.DirectoryEntry  
$strFilter = "(&(objectCategory=computer)(name=" + $env:computername + "))"

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher  
$objSearcher.SearchRoot = $objDomain  
$objSearcher.Filter = $strFilter

$strPath = $objSearcher.FindOne().Path

From the result in $strPath, you should be able to build a network path to the share you need.

  • Hi, Thanks for this, it did point me in the right direction. However, I was after the user and the OU of the user, not the computer. As it happens, your solution just needed a few tweaks to be what I was after so thanks! – Frans Sep 10 '09 at 11:10
  • My fault, I understood you were after the OU of the computer... getting the user OU is quite similar, though. – Massimo Sep 10 '09 at 13:37

To actually return an object of type Microsoft.ActiveDirectory.Management.ADOrganizationalUnit of a User Object, use this:

Get-ADOrganizationalUnit -Identity $(($adUser = Get-ADUser -Identity $env:USERNAME).DistinguishedName.SubString($adUser.DistinguishedName.IndexOf("OU=")))

The same can be accomplished for a computer:

Get-ADOrganizationalUnit -Identity $(($adComputer = Get-ADComputer -Identity $env:COMPUTERNAME).DistinguishedName.SubString($adComputer.DistinguishedName.IndexOf("OU=")))

I eventually figured out how to get the OU from http://www.microsoft.com/technet/scriptcenter/resources/pstips/dec07/pstip1207.mspx and thought I would share the results. The code to get the OUs is this:

$strName = $env:username
$strFilter = "(&(objectCategory=User)(samAccountName=$strName))"

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.Filter = $strFilter

$objPath = $objSearcher.FindOne()
$objUser = $objPath.GetDirectoryEntry()

However, I then had a realisation that using the OU would be fraught with difficulty as a user may be a member of multiple OU's. I therefore decided to use the Company field in the User object. From the same code above I can do

$strCompany = $objUser.Company   

And then do my drive mapping.

This answers my original question, but just for interest I should mention that I then decided not to use PowerShell for the logon script after all: It is just too painful to deploy; You can't just put a .ps1 file in the Group Policy, you have to explicitly call Powershell.exe from a cmd file. So, I rewrote my script in vbscript in a few minutes and regretted that I hadn't started with that :)


However, I then had a realisation that using the OU would be fraught with difficulty as a user may be a member of multiple OU's

No. It can't. Per defintion a user account can only reside at one place in AD. Different users however, may have the same RDN.


I used this to get the ou

$hostname = hostname

$hostnameobject = get-adcomputer $hostname

$ou = $hostnameobject.distinguishedname


Find the DN of the User and parse it from that.

I'm not a Powershell expert, or even an amateur for that matter, but that's probably where I'd start.

Sorry I can't be of any more assistance than that, hopefully it's some food for thought.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.