none
How to combine the Get-ADUser Powershell script to produce single result? RRS feed

  • Question

  • People,

    I need some help in combining the two Power-Shell command to generate the CSV result with the column as follows:

    Mailbox Location, Name, PrimarySmtpAddress, OU
    OnPremise, OnPremise User1, OnPremise.User1@domain.com, domain.com/Users/Exchange 2013
    OnPremise, OnPremise User2, OnPremise.User2@domain.com, domain.com/Users/Exchange 2013

    Office365, Office365 User1, Office365.User1@domain.com, domain.com/Users/Exchange Online
    Office365, Office365 User2, Office365.User2@domain.com, domain.com/Users/Exchange Online

    The below two script works fine when I executed manually, but not sure how to combine it together so I can get the definitive list of who still On-premise and who is in Office 365 already.

    Script to get the On-premise Users based on HomeMDB AD attributes:

    Get-ADUser -ResultSetSize 99999 -Filter * -Properties homeMDB, msExchRemoteRecipientType, msExchRecipientDisplayType, msExchRecipientTypeDetails, proxyAddresses | Where-Object { $_.homeMDB -ne $null }

    Script to get the Migrated Users based on Multiple AD attributes:

    Get-ADUser -ResultSetSize 99999 -Filter * -Properties homeMDB, msExchRemoteRecipientType, msExchRecipientDisplayType, msExchRecipientTypeDetails, proxyAddresses | Where-Object { (($_.msExchRemoteRecipientType -eq 4) -or ($_.msExchRemoteRecipientType -eq 20) -or ($_.msExchRemoteRecipientType -eq 21) -or ($_.msExchRemoteRecipientType -eq 100)) -and #( $_.msExchRecipientDisplayType -eq '-2147483642') -and (($_.msExchRecipientTypeDetails -eq '2147483648') -or (($_.msExchRecipientTypeDetails -eq '34359738368'))) -and ( $_.proxyAddresses -match "onmicrosoft.com") } Thank you in advance.


    /* Server Support Specialist */

    Wednesday, June 19, 2019 3:02 AM

Answers

  • Get-ADUser -ResultSetSize 99999 -Filter * -Properties homeMDB, msExchRemoteRecipientType, msExchRecipientDisplayType, msExchRecipientTypeDetails,proxyAddresses `
    | Where-Object { $_.homeMDB -ne $null } `
    | Select homeMDB, msExchRemoteRecipientType, msExchRecipientDisplayType, msExchRecipientTypeDetails,proxyAddresses `
    | Export-Csv "OutFile.csv" -Append -NoTypeInformation
    
    Get-ADUser -ResultSetSize 99999 -Filter * -Properties homeMDB, msExchRemoteRecipientType, msExchRecipientDisplayType, msExchRecipientTypeDetails,proxyAddresses `
    | Where-Object {(($_.msExchRemoteRecipientType -eq 4) -or ($_.msExchRemoteRecipientType -eq 20) -or ($_.msExchRemoteRecipientType -eq 21) -or ($_.msExchRemoteRecipientType-eq 100)) -and (($_.msExchRecipientTypeDetails -eq '2147483648') -or (($_.msExchRecipientTypeDetails -eq '34359738368'))) -and( $_.proxyAddresses -match "onmicrosoft.com")} `
    | Select homeMDB, msExchRemoteRecipientType, msExchRecipientDisplayType, msExchRecipientTypeDetails,proxyAddresses `
    | Export-Csv "OutFile.csv" -Append -NoTypeInformation
    This should be quick & easy way.

    You have been answering questions and marking them as possible answers all morning.  Most of the time you have not read the full question and your proposed code is mostly wrong or misleading. Perhaps you should take some time to read the questions, issues and other answers before adding your own guess.

    When answering new users with little experience adding bad and poorly though out answers is very misleading for those trying to learn.

    In this answer you should not use and don't need line extenders.  Those were deprecated after PS 1.  Your answer also does not resolve the original issue.

    The following is an example of how to write this code so that is readable and so that it can be debugged and edited easily.

    $properties = 'homeMDB,msExchRemoteRecipientType,msExchRecipientDisplayType,msExchRecipientTypeDetails,proxyAddresses' -split ','
    Get-ADUser -Filter * -Properties $properties |
        Where-Object{$_.homeDB} |
    Select-Object $properties | Export-Csv OutFile.csv -NoTypeInformation Get-ADUser -ResultSetSize -Filter * -Properties $properties | Where-Object { $_.proxyAddresses -match 'onmicrosoft.com' -and $_.msExchRemoteRecipientType -in @{4,20,21,100) -and $_.msExchRecipientTypeDetails -in @(2147483648,34359738368) } | Select-Object $properties | Export-Csv OutFile.csv -Append -NoTypeInformation

    Note that this code is readable and does not use line extenders. It also takes advantage of the rich set of operators that can greatly simplify code.

    This code does not solve all of the issues on the OPs shopping list but the rest can now be easily added.

    Here is a good resource to start with to learn how to format code for usability.

    PowerShell Style Guidelines


    \_(ツ)_/






    Wednesday, June 26, 2019 1:40 PM
    Moderator

All replies

  • Hi,

    Thanks for your question.

    If your two script output object has same headers(property names), you can just add them.

    For example:

    $1=Get-ADUser -ResultSetSize 99999 -Filter * -Properties homeMDB, msExchRemoteRecipientType, msExchRecipientDisplayType, msExchRecipientTypeDetails,proxyAddresses |Where-Object { $_.homeMDB -ne $null }
    $2=Get-ADUser -ResultSetSize 99999 -Filter * -Properties homeMDB, msExchRemoteRecipientType, msExchRecipientDisplayType, msExchRecipientTypeDetails,proxyAddresses |Where-Object {(($_.msExchRemoteRecipientType -eq 4) -or ($_.msExchRemoteRecipientType -eq 20) -or ($_.msExchRemoteRecipientType -eq 21) -or ($_.msExchRemoteRecipientType-eq 100)) -and (($_.msExchRecipientTypeDetails -eq '2147483648') -or (($_.msExchRecipientTypeDetails -eq '34359738368'))) -and( $_.proxyAddresses -match "onmicrosoft.com")}
    $3= $1+ $2 
    $3 | Export-Csv "c:\temp\test.csv"

    Best regards,

    Lee


    Just do it.

    Wednesday, June 19, 2019 6:09 AM
    Moderator
  • The question is actually a bit vague.  Just combining the output will create a huge number of duplicate entries.

    The way to do this is to combine there filter statements.  JUst add the first filter to the second as the first item.

    When testing for a value it is always a bad idea to test for $null.  Just test the value

    Where-Object { $_.homeMDB }

    This will only be true if the property contains a value.

    Now just add that to the filter of the second Get-AdUser command.


    \_(ツ)_/

    Wednesday, June 19, 2019 12:47 PM
    Moderator
  • The question is actually a bit vague.  Just combining the output will create a huge number of duplicate entries.

    The way to do this is to combine there filter statements.  JUst add the first filter to the second as the first item.

    When testing for a value it is always a bad idea to test for $null.  Just test the value

    Where-Object { $_.homeMDB }

    This will only be true if the property contains a value.

    Now just add that to the filter of the second Get-AdUser command.


    \_(ツ)_/

    Jrv,

    Thanks for the suggestion, how to do that and combine the script please?


    /* Server Support Specialist */

    Wednesday, June 19, 2019 1:20 PM
  • Get-ADUser -ResultSetSize 99999 -Filter * -Properties homeMDB, msExchRemoteRecipientType, msExchRecipientDisplayType, msExchRecipientTypeDetails,proxyAddresses `
    | Where-Object { $_.homeMDB -ne $null } `
    | Select homeMDB, msExchRemoteRecipientType, msExchRecipientDisplayType, msExchRecipientTypeDetails,proxyAddresses `
    | Export-Csv "OutFile.csv" -Append -NoTypeInformation
    
    Get-ADUser -ResultSetSize 99999 -Filter * -Properties homeMDB, msExchRemoteRecipientType, msExchRecipientDisplayType, msExchRecipientTypeDetails,proxyAddresses `
    | Where-Object {(($_.msExchRemoteRecipientType -eq 4) -or ($_.msExchRemoteRecipientType -eq 20) -or ($_.msExchRemoteRecipientType -eq 21) -or ($_.msExchRemoteRecipientType-eq 100)) -and (($_.msExchRecipientTypeDetails -eq '2147483648') -or (($_.msExchRecipientTypeDetails -eq '34359738368'))) -and( $_.proxyAddresses -match "onmicrosoft.com")} `
    | Select homeMDB, msExchRemoteRecipientType, msExchRecipientDisplayType, msExchRecipientTypeDetails,proxyAddresses `
    | Export-Csv "OutFile.csv" -Append -NoTypeInformation
    
    This should be quick & easy way.
    Wednesday, June 26, 2019 1:16 PM
  • Get-ADUser -ResultSetSize 99999 -Filter * -Properties homeMDB, msExchRemoteRecipientType, msExchRecipientDisplayType, msExchRecipientTypeDetails,proxyAddresses `
    | Where-Object { $_.homeMDB -ne $null } `
    | Select homeMDB, msExchRemoteRecipientType, msExchRecipientDisplayType, msExchRecipientTypeDetails,proxyAddresses `
    | Export-Csv "OutFile.csv" -Append -NoTypeInformation
    
    Get-ADUser -ResultSetSize 99999 -Filter * -Properties homeMDB, msExchRemoteRecipientType, msExchRecipientDisplayType, msExchRecipientTypeDetails,proxyAddresses `
    | Where-Object {(($_.msExchRemoteRecipientType -eq 4) -or ($_.msExchRemoteRecipientType -eq 20) -or ($_.msExchRemoteRecipientType -eq 21) -or ($_.msExchRemoteRecipientType-eq 100)) -and (($_.msExchRecipientTypeDetails -eq '2147483648') -or (($_.msExchRecipientTypeDetails -eq '34359738368'))) -and( $_.proxyAddresses -match "onmicrosoft.com")} `
    | Select homeMDB, msExchRemoteRecipientType, msExchRecipientDisplayType, msExchRecipientTypeDetails,proxyAddresses `
    | Export-Csv "OutFile.csv" -Append -NoTypeInformation
    This should be quick & easy way.

    You have been answering questions and marking them as possible answers all morning.  Most of the time you have not read the full question and your proposed code is mostly wrong or misleading. Perhaps you should take some time to read the questions, issues and other answers before adding your own guess.

    When answering new users with little experience adding bad and poorly though out answers is very misleading for those trying to learn.

    In this answer you should not use and don't need line extenders.  Those were deprecated after PS 1.  Your answer also does not resolve the original issue.

    The following is an example of how to write this code so that is readable and so that it can be debugged and edited easily.

    $properties = 'homeMDB,msExchRemoteRecipientType,msExchRecipientDisplayType,msExchRecipientTypeDetails,proxyAddresses' -split ','
    Get-ADUser -Filter * -Properties $properties |
        Where-Object{$_.homeDB} |
    Select-Object $properties | Export-Csv OutFile.csv -NoTypeInformation Get-ADUser -ResultSetSize -Filter * -Properties $properties | Where-Object { $_.proxyAddresses -match 'onmicrosoft.com' -and $_.msExchRemoteRecipientType -in @{4,20,21,100) -and $_.msExchRecipientTypeDetails -in @(2147483648,34359738368) } | Select-Object $properties | Export-Csv OutFile.csv -Append -NoTypeInformation

    Note that this code is readable and does not use line extenders. It also takes advantage of the rich set of operators that can greatly simplify code.

    This code does not solve all of the issues on the OPs shopping list but the rest can now be easily added.

    Here is a good resource to start with to learn how to format code for usability.

    PowerShell Style Guidelines


    \_(ツ)_/






    Wednesday, June 26, 2019 1:40 PM
    Moderator