How do I get a list of "Manager" and "Department" using Powershell? AD or Exchange.
-
Monday, November 26, 2012 7:00 PM
I'm looking to get a simple printout of all users with a custom attribute. Included in this printout I need the Manager and Department displayed. How do I do this?
In Exchange, I've tried this:
Get-Mailbox -filter 'CustomAttribute15 -eq "cee contact"' | select Name, Alias, customAttribute3, manager, department
This prints out my Name, Alias, and customAttribute3 with no problems, however, the manager and department are blank. It appears that Exchange PowerShell does not want to list this information.
I've tried playing around with a few powershell commands with Get-ADUser in Active Directory powershell, but still not getting the Manager or Department (in fact, I can't even figure out how to list the customAttribute3 in AD PS).
Is there a powershell command (in either Exchange or AD) I can use to get the Manager and Department info listed? Any help/direction would be appreciated!
Thank you,
Elizabeth
All Replies
-
Monday, November 26, 2012 7:40 PMModerator
You'll need to combine selected properties from 2 different Exchange cmdlets - get-mailbox and get-user.
Not tested, but I believe this should work:
$MBXs = Get-Mailbox -filter 'CustomAttribute15 -eq "cee contact"' | select Identity,Name, Alias, customAttribute3 foreach ($MBX in $MBXs){ $User = Get-User $MBX.Identity New-Object psobject -Property @{ Name = $MBX.Name Alias = $MBX.Alias customAttribute3 = $MBX.customAttribute3 Manager = $User.Manager Department = $User.Deparetment } }[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
- Marked As Answer by ElizabethCEE2010 Monday, November 26, 2012 9:19 PM
-
Monday, November 26, 2012 9:19 PM
This worked great, thank you!
One last question - where do I put my Out-File command to get this exported as a txt? CSV is fine too, if that's easier.
-
Monday, November 26, 2012 9:25 PMModerator
Just pipe it out at the end:
$MBXs = Get-Mailbox -filter 'CustomAttribute15 -eq "cee contact"' | select Identity,Name, Alias, customAttribute3 foreach ($MBX in $MBXs){ $User = Get-User $MBX.Identity New-Object psobject -Property @{ Name = $MBX.Name Alias = $MBX.Alias customAttribute3 = $MBX.customAttribute3 Manager = $User.Manager Department = $User.Deparetment } } | export-csv c:\somedir\mylist.csv -notype[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
-
Monday, November 26, 2012 9:29 PM
I tried that, but I get an error:
An empty pipe element is not allowed. At line:12 char:8 + } | <<<< export-csv C:\Users\elizabeth.million\Desktop\mylist.csv -notyp e + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx ception + FullyQualifiedErrorId : EmptyPipeElement -
Monday, November 26, 2012 9:38 PMModerator
Sorry, my fault.
$MBXs = Get-Mailbox -filter 'CustomAttribute15 -eq "cee contact"' | select Identity,Name, Alias, customAttribute3 &{foreach ($MBX in $MBXs){ $User = Get-User $MBX.Identity New-Object psobject -Property @{ Name = $MBX.Name Alias = $MBX.Alias customAttribute3 = $MBX.customAttribute3 Manager = $User.Manager Department = $User.Deparetment } } }| export-csv c:\somedir\mylist.csv -notype[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
-
Monday, November 26, 2012 9:43 PMModerator
Hi,
Try it this way:
& { foreach ($MBX in $MBXs) { $User = Get-User $MBX.Identity New-Object psobject -Property @{ Name = $MBX.Name Alias = $MBX.Alias customAttribute3 = $MBX.customAttribute3 Manager = $User.Manager Department = $User.Deparetment } } } | export-csv c:\somedir\mylist.csv -notype
That is, enclose the foreach statement block within { } to turn it into a scriptblock, and use the invoke operator (&) to invoke it.
Bill
-
Monday, November 26, 2012 9:50 PMModeratorI think i've seen that somewhere before :).
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
-
Monday, November 26, 2012 9:52 PM
I got everything I needed from mjolinor's modified code. Thanks again!
Thanks for the alternative, AbqBill, much appreciated.
Cheers,
Elizabeth

