Last Login in Active Directory
To search the entire domain for a user's last login in Active Directory using PowerShell, you can use the Get-ADUser cmdlet with the -Filter parameter.
Get-ADUser -Filter {SamAccountName -eq "username"} -Properties lastLogon | Select-Object Name, SamAccountName, @{Name="LastLogon"; Expression={[DateTime]::FromFileTime($_.lastLogon)}} | Export-CSV -Path "user_last_login.csv" -NoTypeInformation
This PowerShell code retrieves the last logon information for a user account in Active Directory and exports the result to a CSV file.
The Get-ADUser cmdlet with the -Filter parameter is used to search for a user with a specific SamAccountName in Active Directory. The lastLogon attribute is then retrieved using the -Properties parameter.
The results are then piped to the Select-Object cmdlet, which is used to display only the Name, SamAccountName, and LastLogon properties of the user object. The LastLogon property is converted to a human-readable format using the [DateTime]::FromFileTime() method.
Finally, the Export-CSV cmdlet is used to export the search results to a CSV file named "user_last_login.csv" in the current directory. The -NoTypeInformation parameter is used to exclude the type information from the CSV file.