I haven’t written and PowerShell script in a while…

I want to write a ps1 script that will generate a CSV with the following:

All mail users

Mail users’ job titles

The last time each mail user logged in

This is for Office 365.

(I was able to generate two individual CSV files and combine them, but it was too time consuming!)

I thought I would use Get-MailBox and Get-MailboxStatistics to get the display name and last logon time, and I can use Get-AzureADUser to get the job title.

If I used Get-MailBox and Get-MailboxStatistics together to output to a CSV, then I could use the CSV file to get the usernames for Get-AzureADUser, and then output the display name, job title, and last logon to a new CSV, and then delete the original CSV.

Where I need help is with getting variables from the first CSV and with the looping the script.

Below is some ps1 and pseudocode

mkdir c:\scripts
Get-MailBox -ResultSize unlimited | Get-MailboxStatistics | Select DisplayName, LastLogonTime | Export-Csv c:\scripts\last_logon_TEMP.csv

#pseudo code
# $DisplayName = column A from  c:\scripts\last_logon_TEMP.csv
# $LastLogon = column B from  c:\scripts\last_logon_TEMP.csv
# for 
#   each DisplayName, 
#    $JobTitle = Get-AzureADUser $DisplayName | Select JobTitle >> $DisplayName,$JobTitle,$LastLogon >> c:\scripts\name_title_lastlogon.csv
# end

del c:\scripts\last_logon_TEMP.csv

@Microsoft

7 Spice ups

If you install the MSOnline module, you can use Get-MsolUser, which should tell you the information that you want.

Thank you for the reply! Unfortunatly, when I ran the following, last logon time was not one of the values in the output - only the time the user was created, the last time the password was changed, and the “StsRefreshTokensValidFrom”

Get-MsolUser -UserPrincipalName jdoe@[domain].com | select *

I was able to answer my own question

first there was a powershell command piped through Export-Csv

Then brought back in to the script using $OldCsv=IMPORT-CSV and then :

echo "heading1, heading2, heading3" > new.csv
FOREACH ($item in $OldCsv) {
  $newVariable=$Item.OldVariable
  $newVariable2=$Item.OldVariable2+
  $Item.OldVariable3
  $newVariable3=OldVariable4
  Echo newVariable2","newVariable2","newVariable3"," >> new.csv
}

FOREACH ($item in $OldCsv) {

$newVariable=$Item.OldVariable

$newVariable2=$Item.OldVariable2+ $Item.OldVariable3

$newVariable3=OldVariable4

Echo newVariable2",“newVariable2”,“newVariable3”," >> new.csv

}