Search code examples
powershelloutlook

Add Output of two Powershell commands into an Array


I've been trying to figure out if this is even possible in Powershell. I'm trying to pull a list of all users and their email addresses into an array from the Outlook Global Address List so the resulting output would look like this:

Alice Peters [email protected]
Bob Jackson [email protected]
etc

I'm currently able to pull the information using:

[Microsoft.Office.Interop.Outlook.Application] $galSearch = New-Object -ComObject Outlook.Application

$search = @($galSearch.Session.GetGlobalAddressList().AddressEntries | ForEach-Object {$_.GetExchangeUser().Name; $_.GetExchangeUser().PrimarySmtpAddress})

$search

As you might expect the result is formatted like this:

Alice Peters
[email protected]
Bob Jackson
[email protected]
etc

Is there a way to have them be in pairs and not one above the other?


Solution

  • My Outlook doesn't have any content in the GAL so I can't verify my suggestion.

    If the object types supports string handling, just joint the two results into one string.

    ForEach-Object {
        $_.GetExchangeUser().Name + ' ' +  
        $_.GetExchangeUser().PrimarySmtpAddress
    }
    

    Otherwise, try converting to strings first.

    ForEach-Object {
        ($_.GetExchangeUser().Name).ToString() + ' ' +  
        ($_.GetExchangeUser().PrimarySmtpAddress).ToString()
    }
    

    Or you could go for creating hashtables instead.

    Or, the simplest solution as Mathias pointed out in his comment, go for just selecting the properties. That will return an object instead of arrays.