title: Getting Active Directory Domain Services Folder and/or DistingiushedName
body: <p>I'm new to scripting with Powershell for Active Directory, and attempting to pull out users in a list and enumerating their groups; however when I do so the resultant information contains just the name of the group which is duplicated under many differently named OUs. Looking at them in AD Users and Computers under the Member Of tab shows the Name and the Active Directory Domain Services Folder which contains exactly the differentiating info I need, or alternately I could use the DistinguishedName which isn't as nicely formatted for readability but would also work.</p>
  
  <p>Problem with simplified examples: If a group name is the same across different OUs (like "TestUsers") then the script currently dumps multiple group names without differentiation "TestUsers, TestUsers, TestUsers" instead of showing the underlying OUs in a clean format "Michigan\TestUsers, NewYork\TestUsers" or Distinguished Name format "CN=TestUsers,CN=Michigan,etc";"CN=TestUsers,CN=NewYork,etc".</p>
  
  <pre><code>$alist = "Name`tAccountName`tDescription`twhenCreated`tAcctEnabled`tGroups`n"
  $userlist = Get-ADUser -SearchBase "OU=Service Accounts,OU=Information Systems,DC=conteso,DC=local" -Filter * -Properties * | Select-Object -Property Name,SamAccountName,Description,EmailAddress,LastLogonDate,Manager,Title,Department,Company,whenCreated,Enabled,MemberOf | Sort-Object -Property Name
  $userlist | ForEach-Object {
      $grps = $_.MemberOf | Get-ADGroup | ForEach-Object {$_.Name} | Sort-Object
      $arec = $_.Name,$_.SamAccountName,$_.Description,$_.whenCreated,$_.Enabled
      $aline = ($arec -join "`t") + "`t" + ($grps -join "`t") + "`n"
      $alist += $aline
  }
  $alist | Out-File C:\psscripts\service_accounts_groups.csv
  </code></pre>
  
  <p>Any help is appreciated!</p>