Convert a HashTable to an Array

Of course, this only works with a single column in the hashtable…

Say you have a list of mailboxes in a hashtable, like this:

$Mailboxes = Get-Mailboxes -OrganizationalUnit administration

But for some reason (It doesn’t really matter why, does it? You just do.) you want a list of their User Principal Names in an array instead of the hashtable. You can do this like so:

$Array = @()
$Mailboxes | Foreach {$Array += $_.UserPrincipalName}

Now if you check your @Array, you’ll find something like this:

jdoe@domain.com
amouse@domain.com
flast@domain.com

(You might be thinking that email address seems a much more obvious target for this example. Try the PrimarySmtpAddress or WindowsEmailAddress attributes. You’ll see why I didn’t use them. You can still do it, but it’s trickier. Let’s save that one for another day.)

Leave a Reply

Your email address will not be published. Required fields are marked *