Our edu-tenant contains 40k users. I have a list of 800 UPN’s who our Identity Team wants to know what their last logondate was.<\/p>\n
I’ve found a script to export all users, but i’m struggling to get a foreach from the excel file.<\/p>\n
This is what i have so far:<\/p>\n
$csv = \"c:\\temp\\LastLogons_$(Get-Date -format dd_MM_yyyy).csv\"\n$users = get-mailbox -ResultSize Unlimited | select UserPrincipalName\nForeach ($user in $users){\n$mbx = get-mailboxstatistics -Identity $($user.UserPrincipalName) | Select LastLogonTime\n$upn = $user.UserPrincipalName\nif ($mbx.LastLogonTime -eq $null){\n$res = \"Never\"\n}else{\n$res = $mbx.LastLogonTime\n}\n$outStr = \"$upn,$res\"\nOut-File -FilePath $csv -InputObject $outStr -Encoding UTF8 -append\n}\n<\/code><\/pre>\nCan anybody help me out? Pretty pls?<\/p>","upvoteCount":6,"answerCount":9,"datePublished":"2021-03-19T08:43:13.000Z","author":{"@type":"Person","name":"doncorleo","url":"https://community.spiceworks.com/u/doncorleo"},"acceptedAnswer":{"@type":"Answer","text":"
Still had difficulties with the foreach loop. I need training…
\nI used Get-MailboxStatistics -Identity “UPN” | Select LastLogonTime and combined the csv and the script, leaving me with just pasting it in Powershell.<\/p>\n
After it ran, i pasted the results in an Excel file. Problem solved
<\/p>","upvoteCount":0,"datePublished":"2021-03-22T13:57:39.000Z","url":"https://community.spiceworks.com/t/lastlogondate-from-a-list-of-users/794288/9","author":{"@type":"Person","name":"doncorleo","url":"https://community.spiceworks.com/u/doncorleo"}},"suggestedAnswer":[{"@type":"Answer","text":"
Our edu-tenant contains 40k users. I have a list of 800 UPN’s who our Identity Team wants to know what their last logondate was.<\/p>\n
I’ve found a script to export all users, but i’m struggling to get a foreach from the excel file.<\/p>\n
This is what i have so far:<\/p>\n
$csv = \"c:\\temp\\LastLogons_$(Get-Date -format dd_MM_yyyy).csv\"\n$users = get-mailbox -ResultSize Unlimited | select UserPrincipalName\nForeach ($user in $users){\n$mbx = get-mailboxstatistics -Identity $($user.UserPrincipalName) | Select LastLogonTime\n$upn = $user.UserPrincipalName\nif ($mbx.LastLogonTime -eq $null){\n$res = \"Never\"\n}else{\n$res = $mbx.LastLogonTime\n}\n$outStr = \"$upn,$res\"\nOut-File -FilePath $csv -InputObject $outStr -Encoding UTF8 -append\n}\n<\/code><\/pre>\nCan anybody help me out? Pretty pls?<\/p>","upvoteCount":6,"datePublished":"2021-03-19T08:43:13.000Z","url":"https://community.spiceworks.com/t/lastlogondate-from-a-list-of-users/794288/1","author":{"@type":"Person","name":"doncorleo","url":"https://community.spiceworks.com/u/doncorleo"}},{"@type":"Answer","text":"
This could do it for you:<\/p>\n
$Path = 'C:\\Foo\\LastLogon.csv'\nGet-ADUser -Filter {Enabled -eq $true} -Properties LastLogonTimeStamp | \n \nSelect-Object Name,@{Name=\"Stamp\"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp).ToString('yyyy-MM-dd_hh:mm:ss')}} | Export-Csv -Path $Path –notypeinformation\n<\/code><\/pre>\ndo this and you see the following in excel:<\/p>\n