Powershell to get UPN from EMPID CSV import then export to csv even if not found

Hello everyone, Just getting into powershell and was trying to do a search of users in our o365 to find their email and export out to csv so I can copy them to new sheet as needed. Issue I have is if they don’t have mail it skips them leaving blanks so when copy back I’m not lined up correctly. I’m thinking I need to do a IF or Try but unsure how this should be. My CSV import file is 1 column with samaccountname as title and Emp numbers in that column.

Example C:\Temp\Emp_ID_Import.csv

Samaccountname

Dave

Sue

Bob

Now say Sue has no email. when I run this code.

$empid = import-csv c:\Temp\Emp_ID_Import.csv

$empid | foreach{Get-Mailbox -Identity $_.samaccountname}| Select Name, Alias, UserP* | Export-Csv C:\PSExport\MailEXP.csv -NoTypeInformation

I will have file that will list Dave then bob along with Selected fields. I’m looking to have it add Sue in there with nothing else just so I will know that they have no mail.

Sure this is simple just not seeing it. Hoping tolearn and then be able to expand out using this step for future Scripts.

Thanks for help in Advance.

This seems to produce the results you describe. The input is saved in a “SamAccountName_Input” property and then if the Get-Mailbox query succeeds the results of that are added to the object.

https://gist.github.com/jeremytbrun/e08f7e52ae5a5a15035017ac1f210a56