none
How do I get a list of "Manager" and "Department" using Powershell? AD or Exchange.

    Question

  • 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

    Monday, November 26, 2012 7:00 PM

Answers

  • 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 " "

    Monday, November 26, 2012 7:40 PM
    Moderator

All replies

  • 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 " "

    Monday, November 26, 2012 7:40 PM
    Moderator
  • 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:19 PM
  • 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:25 PM
    Moderator
  • 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:29 PM
  • 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:38 PM
    Moderator
  • 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:43 PM
    Moderator
  • I think i've seen that somewhere before :).

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Monday, November 26, 2012 9:50 PM
    Moderator
  • I got everything I needed from mjolinor's modified code.  Thanks again!

    Thanks for the alternative, AbqBill, much appreciated.

    Cheers,
    Elizabeth

    Monday, November 26, 2012 9:52 PM
  • WARNING: By default, only the first 1000 items are returned. Use the ResultSize parameter to specify the number of
    items returned. To return all items, specify "-ResultSize Unlimited". Be aware that, depending on the actual number of
    items, returning all items can take a long time and consume a large amount of memory. Also, we don't recommend storing
    the results in a variable. Instead, pipe the results to another task or script to perform batch changes.
    Thursday, August 07, 2014 6:31 PM