# Adding users and managers departments to existing script

**URL:** <https://forums.powershell.org/t/adding-users-and-managers-departments-to-existing-script/20187>\
**Category:** PowerShell Help\
**Created:** [August 16, 2022, 7:42pm UTC](https://forums.powershell.org/t/adding-users-and-managers-departments-to-existing-script/20187 "2022-08-16T19:42:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomlon](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tomlon](https://forums.powershell.org/u/tomlon)\
**Post date:** [August 16, 2022, 7:42pm UTC](https://forums.powershell.org/t/adding-users-and-managers-departments-to-existing-script/20187/1 "2022-08-16T19:42:26Z")

</div>

I have aa script that outputs user infomration from a collection of security groups. With assistance from this group, I was able to add the users’ Manager and manager email, as well as L2 Manager and L2 Manager email. Well, now I am now needing to include the user’s Department, as well as the L2 managers Department. I am still trying to learn the properties and how to extract that information. Any help is appreciated. I have included the section of the script I am using to extract what is currently working.

```auto
$AllMembers = foreach($ADGroup in $ADgroups) {
	#Get-ADGroupMember -Identity $group | Select SamAccountName, Name, Manager
	Get-ADGroupMember -Identity $ADgroup | Select -ExpandProperty DistinguishedName 
	}
	
# Removes group names, duplicate IDs and adds manager
$AllMembersData = $AllMembers | Get-ADObject | ?{$_.objectclass -eq 'user'}

#USerDetails
$Level1Managers = @{}
$Level2Managers = @{}

$NoManagerDetails = @{
    Name = 'N/A'
    Mail = 'N/A'
}
$Level1Managers.Add('NoManager',$NoManagerDetails)
$Level2Managers.Add('NoManager',$NoManagerDetails)

$UserDetails = foreach ($Member in $AllMembersData) {
   $User = Get-ADUser $Member -Properties Mail,Manager
   if ($User.Manager) {
       $L1Manager = $User.Manager
        if ($L1Manager -notin $Level1Managers.Keys) {
            $L1ManagerDetails = Get-ADUser $L1Manager -Properties Mail,Manager
            $Level1Managers.Add($L1Manager,$L1ManagerDetails)
        }
        if ($Level1Managers.$($L1Manager).Manager) {
          $L2Manager = $Level1Managers.$($L1Manager).Manager
            if ($L2Manager -notin $Level2Managers.Keys) {
                $L2ManagerDetails = Get-ADUser $L2Manager -Properties Mail,Manager
                $Level2Managers.Add($L2manager,$L2ManagerDetails)
            }
        }
        else {
            $L2Manager = 'NoManager'
        }
    }
    else {
        $L1Manager = 'NoManager'
        $L2Manager = 'NoManager'
    }
    [PSCustomObject] @{
        Name = $User.Name
        sAMAccountName = $User.SamAccountName
		Enabled = $User.Enabled
        Mail = $User.Mail
        Manager = $Level1Managers.$($L1Manager).Name
        ManagerEmail = $Level1Managers.$($L1Manager).Mail
        L2Manager = $Level2Managers.$($L2Manager).Name 
        L2ManagerEmail = $Level2Managers.$($L2Manager).Mail  
		   
    }
}

$UserDetailsUniq = $UserDetails | Select-Object sAMAccountName, Name, Mail, Enabled, Manager, ManagerEmail, L2Manager, L2ManagerEmail,Department | Sort-Object -Property sAMAccountName -Unique

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 16, 2022, 8:22pm UTC](https://forums.powershell.org/t/adding-users-and-managers-departments-to-existing-script/20187/2 "2022-08-16T20:22:06Z")

</div>

I’m not sure if I really got what your question is.

> [@tomlon](#):
>
> now I am now needing to include the user’s Department

You simply add the department to the `[PSCustomObject]`. You have to add the property `Department` (or what the proper name is - I don’t have an AD at them moment to check) to the parameter `-Properties` of the cmdlet `Get-AdUser` to actually get the attribute included in the output of your query. But then it is the same as it is with the other attributes you’ve already included. 🤷🏽‍♂️

The same is valid for the other attributes / properties you want to include for your managers.

---

<div class="post-metadata">

**Author:** ![tomlon](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tomlon](https://forums.powershell.org/u/tomlon)\
**Post date:** [August 19, 2022, 12:57pm UTC](https://forums.powershell.org/t/adding-users-and-managers-departments-to-existing-script/20187/3 "2022-08-19T12:57:11Z")

</div>

Thanks. Simply added the -Department to the properties.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/adding-users-and-managers-departments-to-existing-script/20187/4 "2024-05-16T20:16:44Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


