$Result = "C:\Temp\UserListing.csv" $Users = Get-ADUser -searchbase "ou=,dc=" -LDAPFilter "(&(manager=*)(objectCategory=person)(objectClass=user)(mail=*))" $Data =@() foreach ($User in $Users) { $UserDetail = Get-ADUser $User -properties UserPrincipalName, SamAccountName, SID, DistinguishedName, GivenName, Surname, mail, Title, Country, Company, Department, Office, MobilePhone |select UserPrincipalName, SamAccountName, SID, DistinguishedName, GivenName, Surname, mail, Title, Company, Department, Office, MobilePhone $Managersemail = get-aduser (get-aduser $User -properties Manager | Select -ExpandProperty Manager) -properties EmailAddress | Select EmailAddress $Data += [PSCustomObject] @{ header_primary_user_ID_mandatory = $UserDetail.UserPrincipalName header_username_NTLM ='\' + $UserDetail.SamAccountName header_FQDN = $UserDetail.UserPrincipalName header_username = $UserDetail.SamAccountName header_SID_optional = $UserDetail.sid header_UID_optional = $UserDetail.uid header_OU_optional = $UserDetail.DistinguishedName header_first_name_optional = $UserDetail.GivenName header_last_name_optional = $UserDetail.SurName header_primary_email_optional = $UserDetail.mail header_title_optional = $UserDetail.Title header_business_unit_optional = $UserDetail.Company header_department_optional = $UserDetail.Department header_city_optional = $UserDetail.Office header_country_optional = $UserDetail.country header_company_optional = $UserDetail.Company header_manager_primary_user_ID_optional = $Managersemail.EmailAddress header_dlp_reviewer_primary_user_ID_optional= "" custom1_optional = $UserDetail.MobilePhone custom2_optional = "" custom3_optional = "" } } $Data | Export-Csv $Result -NoTypeInformation