Forum Discussion
dewandtcutterbuckcom
Apr 05, 2023Copper Contributor
Export 365 Users last logon time using powershell
I have created a PowerShell command that is supposed to export every users last logon time that is greater than 1 day. But it continues to create a blank document. Below is the command. Get-Ma...
LainRobertson
Apr 05, 2023Silver Contributor
You're close, but where you've gone wrong is putting everything inside the ForEach-Object block. The Sort-Object and Export-Csv belong outside of that block, not in it.
I'm not sure why your file is empty other than nothing has a LastLogonTime older than one day. Try leaving out the Sort-Object and Export-Csv sections to see if any results are being returned.
Here's an example of how the blocks should be arranged:
Get-EXOMailbox -PropertySets StatisticsSeed -ResultSize unlimited |
Get-EXOMailboxStatistics -PropertySets Minimum -Properties LastLogonTime |
Where-Object {
$_.LastLogonTime -lt [datetime]::Now.AddDays(-1);
} |
Select-Object -Property LastLogonTime, MailboxGuid, DisplayName |
Sort-Object -Property LastLogonTime |
Export-Csv -NoTypeInformation -Path "C:\Data\Temp\blah.csv";
Cheers,
Lain
Edited to account for the -Append switch on the CSV export, which I didn't originally spot.