How to convert Last Logon from Date to number of days since Last Logon, this is my script. Also for users without a mailbox how do I get it to not give me “the user doenst exist” while script is running<\/p>\n
Connect-MsolService
\nConnect-ExchangeOnline
\n$Result=@()
\n$mailboxes = Get-MsolUser -MaxResults 5000 | where islicensed -eq true
\n$totalmbx = $mailboxes.Count
\n$i = 1
\n$mailboxes | ForEach-Object {
\n$i++
\n$mbx = $_
\n$mbs = Get-MailboxStatistics -Identity $mbx.UserPrincipalName | Select LastLogonTime
\nif ($mbs.LastLogonTime -eq $null){
\n$lt = “Never Logged In”
\n}else{
\n$lt = $mbs.LastLogonTime }<\/p>\n
Write-Progress -activity “Processing $mbx” -status “$i out of $totalmbx completed”<\/p>\n
$Result += New-Object PSObject -property @{
\nName = $mbx.DisplayName
\nUserPrincipalName = $mbx.UserPrincipalName
\nlicenses = $mbx.Licenses[0].AccountSkuId
\nLastLogonTime = $lt }
\n}<\/p>\n
$Result | Export-CSV “C:\\O365-LastLogon-Info.csv” -NoTypeInformation -Encoding UTF8<\/p>","upvoteCount":5,"answerCount":3,"datePublished":"2021-11-04T04:46:04.000Z","author":{"@type":"Person","name":"spiceuser-iqxup","url":"https://community.spiceworks.com/u/spiceuser-iqxup"},"suggestedAnswer":[{"@type":"Answer","text":"
How to convert Last Logon from Date to number of days since Last Logon, this is my script. Also for users without a mailbox how do I get it to not give me “the user doenst exist” while script is running<\/p>\n
Connect-MsolService
\nConnect-ExchangeOnline
\n$Result=@()
\n$mailboxes = Get-MsolUser -MaxResults 5000 | where islicensed -eq true
\n$totalmbx = $mailboxes.Count
\n$i = 1
\n$mailboxes | ForEach-Object {
\n$i++
\n$mbx = $_
\n$mbs = Get-MailboxStatistics -Identity $mbx.UserPrincipalName | Select LastLogonTime
\nif ($mbs.LastLogonTime -eq $null){
\n$lt = “Never Logged In”
\n}else{
\n$lt = $mbs.LastLogonTime }<\/p>\n
Write-Progress -activity “Processing $mbx” -status “$i out of $totalmbx completed”<\/p>\n
$Result += New-Object PSObject -property @{
\nName = $mbx.DisplayName
\nUserPrincipalName = $mbx.UserPrincipalName
\nlicenses = $mbx.Licenses[0].AccountSkuId
\nLastLogonTime = $lt }
\n}<\/p>\n
$Result | Export-CSV “C:\\O365-LastLogon-Info.csv” -NoTypeInformation -Encoding UTF8<\/p>","upvoteCount":5,"datePublished":"2021-11-04T04:46:04.000Z","url":"https://community.spiceworks.com/t/convert-last-logon-from-date-to-number-of-days/815941/1","author":{"@type":"Person","name":"spiceuser-iqxup","url":"https://community.spiceworks.com/u/spiceuser-iqxup"}},{"@type":"Answer","text":"
If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n