Author Posts

September 4, 2013 at 8:13 am

Hello Guys

I have a problem causing me headaches – any help is appreciated.

I have a csv file with 3 headers:
client-software-version, client-software, user
11, Outlook, username
(...)

What I have worked up so far:

$users = import-csv test.csv | select user
$email = $users | select-object -ExpandProperty user -ErrorAction 0 | foreach {((get-mailbox $_).EmailAddresses | select-object -first 1).SmtpAddress}

I want to add the $email array to the csv as an additional column. The problem is that not all of these users have mailboxes (Emailaddress) so therefore I need to delete those lines without a mailbox – any ideas how I can do this with a simple code?

Thanks again

September 4, 2013 at 8:52 am

To make sure I understand the properly, you want to both add a new column to the CSV, and remove any rows for users that don't have an email address defined? If so, try this:

Import-Csv -Path test.csv |
ForEach-Object {
    $emailAddress = Get-MailBox $_ | Select-Object -ExpandProperty EmailAddresses | Select-Object -First 1 -ExpandProperty SmtpAddress

    if ($emailAddress -ne $null)
    {
        Add-Member -InputObject $_ -MemberType NoteProperty -Name EmailAddress -Value $emailAddress -PassThru
    }
} |
Export-Csv -Path new.csv -NoTypeInformation

(Note: I haven't tested this code because I don't have an Exchange environment handy, but hopefully it either works as-is or helps you figure out a solution.)

September 5, 2013 at 12:52 am

Exactly, I will test this later at work and give you an update if it worked 🙂
Thanks!