powershel exchange if statement

Good days guys I have some code that I am having an issue with. I am trying to find offices with offices with a value and office without a value. Whats happening with my script below is it will run but only creates the value.csv file and I believe not processing the other elseif statement. Please help

foreach ($mailbox in get-mailbox -OrganizationalUnit "name of OU") {
 if($mailbox.office -ne $null) { `
 select displayname, office | export-csv c:\temp\values.csv

}

elseif($mailbox.office -eq $null)   {
	select displayname, office | export-csv c:\temp\novalues.csv

 }

 
}

Open in new window

techdriveAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan McFaddenSystems EngineerCommented:
You don't need the "elseif" call.  Since you only have 2 conditions, you need to only use an "if {} else {}" structure.  Then you can test the value of the office property only once, if the condition is true (matches) the test, go into the brackets, if the condition tests false, it will fall into the "else" brackets.

OR... Why not just put the whole result set into a CVS and use Excel to filter the results?

Dan
footechCommented:
I think Dan's note is a good idea.  In addition, you need to actually send something to the Select-Object command, and when using Export-CSV within a foreach loop you have to append to a file or else the file will be overwritten each time.
foreach ($mailbox in get-mailbox -OrganizationalUnit "name of OU") {
    if($mailbox.office -ne $null) { `
        $mailbox | select displayname, office | export-csv c:\temp\values.csv -Append
    }
    else {
	    $mailbox | select displayname, office | export-csv c:\temp\novalues.csv -Append
    }
}

Open in new window

Another approach would be to collect the results into a couple variables and then output those to file outside of the foreach loop, then you wouldn't need the -append parameter (in case you don't have PS 3.0+).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
techdriveAuthor Commented:
many thanks footech
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.