The Manager field is tricky. Check out my script to see how I went about it:
https://github.com/Alaknar/PowershellPublic/blob/main/Find-ADUser\_Public.ps1
Sry for format. I'm on mobile.
You can get the manager like this:
Get-Aduser .... | select-object givenname,surname,userprincipalname,@{Name="Manager";Expression={(get-aduser (Get-ADUser $_.samaccountname -properties
Ofc this is only the Select-Object part.
If it is not helping I can have a look tomorrow at work
may I suggest some little changes (ignoring the formatting issues cause reddit)
$Users = Get-ADUser -filter 'enabled -eq $true' -properties Enabled, GivenName, Surname, EmailAddress, Manager, Title, Organization
$output = foreach($Singleuser in $users)
{
$manager = get-aduser -identity $Singleuser.Manager
[PSCustomObject]@{
FirstName = $Singleuser.GivenName
Lastname = $Singleuser.Surname
UserEmail = $Singleuser.EmailAddress
Phonenumber = $Singleuser.Phonenumber
JobTitle = $Singleuser.Title
Organization = $Singleuser.Organization
Department = $Singleuser.Department}
ManagerName = $manager.name
ManagerEmail = $manager.EmailAddress
}
$output | export-csv -path file.csv -Encoding utf8 -notypeinformation
getting rid of
* `$output = @()` - Not needed
* `$output +=` - expensive operation arrays are fixed size, so it creates a new array, copies everything to that array, adds new item, then deletes old array
* `$output = foreach($Singleuser in $users)` - result of the above 2 steps
* `$output | ConvertTo-Csv -NoTypeInformation | Out-File file.csv -Encoding` - changed to `export-csv` which does all the same without using extra pipelines
* `foreach($Singleuser in $users)` - bad habit to get into cause really easy to mix `$users` and `$user` especially in larger blocks of code
EDIT: could also change
$props = @(
'Enabled'
'GivenName'
'Surname'
'EmailAddress'
'Manager'
'Title'
'Organization'
)
$Users = Get-ADUser -filter 'enabled -eq $true' -properties $props
if you're worried about line length
The Manager field is tricky. Check out my script to see how I went about it: https://github.com/Alaknar/PowershellPublic/blob/main/Find-ADUser\_Public.ps1
In Select-Object, replace "manager" with the following: @{ name = 'Manager';exp={ Get-ADUser $PSItem -Properties manager | Select-Object -ExpandProperty manager | Get-ADUser -properties displayname | Select-Object -expandproperty displayname } } so it'll be like this: Get-ADUser -filter * -properties Enabled, GivenName, Surname, EmailAddress, Manager, Title | sort-object Name | select-object EmailAddress, GivenName, Surname, @{ name = 'Manager';exp={ Get-ADUser $PSItem -Properties manager | Select-Object -ExpandProperty manager | Get-ADUser -properties displayname | Select-Object -expandproperty displayname } }
Sry for format. I'm on mobile. You can get the manager like this: Get-Aduser .... | select-object givenname,surname,userprincipalname,@{Name="Manager";Expression={(get-aduser (Get-ADUser $_.samaccountname -properties Ofc this is only the Select-Object part. If it is not helping I can have a look tomorrow at work
4 spaces will format it properly
[удалено]
Ever heard of `Export-Csv`?
Doesn’t pull the manager into the output in power shell.
Is it that powershell isn't outputting the manager attribute, or is it that the manager attribute within active directory for the user(s) is blank?
[удалено]
may I suggest some little changes (ignoring the formatting issues cause reddit) $Users = Get-ADUser -filter 'enabled -eq $true' -properties Enabled, GivenName, Surname, EmailAddress, Manager, Title, Organization $output = foreach($Singleuser in $users) { $manager = get-aduser -identity $Singleuser.Manager [PSCustomObject]@{ FirstName = $Singleuser.GivenName Lastname = $Singleuser.Surname UserEmail = $Singleuser.EmailAddress Phonenumber = $Singleuser.Phonenumber JobTitle = $Singleuser.Title Organization = $Singleuser.Organization Department = $Singleuser.Department} ManagerName = $manager.name ManagerEmail = $manager.EmailAddress } $output | export-csv -path file.csv -Encoding utf8 -notypeinformation getting rid of * `$output = @()` - Not needed * `$output +=` - expensive operation arrays are fixed size, so it creates a new array, copies everything to that array, adds new item, then deletes old array * `$output = foreach($Singleuser in $users)` - result of the above 2 steps * `$output | ConvertTo-Csv -NoTypeInformation | Out-File file.csv -Encoding` - changed to `export-csv` which does all the same without using extra pipelines * `foreach($Singleuser in $users)` - bad habit to get into cause really easy to mix `$users` and `$user` especially in larger blocks of code EDIT: could also change $props = @( 'Enabled' 'GivenName' 'Surname' 'EmailAddress' 'Manager' 'Title' 'Organization' ) $Users = Get-ADUser -filter 'enabled -eq $true' -properties $props if you're worried about line length
[удалено]
yeah habits (good and bad) are hard to break
The manager field outputs a GUID iirc. You need to do a second ad lookup if you want the managers name and/or email.
you sure, or is it giving you the distinguished name not the friendly/full name