PowerShell

PowerShell: How to find out users last logon (Get-LocalUser)

With the introduction of PowerShell 5.1 new commands for local user administration were introduced. I`m glad to hear that. I don`t like net user.

Discovering Local User Administration Commands

First, make sure your system is running PowerShell 5.1. Open PowerShell and run

(Get-Host).Version

Unbenannt.PNG

The commands can be found by running

Get-Command -Module Microsoft.PowerShell.LocalAccounts

Unbenannt.PNG

Users Last Logon Time

Back to topic. To find out all users, who have logged on in the last 10 days, run

Get-LocalUser | Where-Object {$_.Lastlogon -ge (Get-Date).AddDays(-10)} | Select-Object Name,Enabled,SID,Lastlogon | Format-List

Unbenannt.PNG

To search for users, who have not logged on in the last 30 days, run

Get-LocalUser | Where-Object {$_.Lastlogon -le (Get-Date).AddDays(-30)} | Select-Object Name,Enabled,SID,Lastlogon | Format-List

Other examples

Creating users:

New-LocalUser -Name Peter -Password (ConvertTo-SecureString "Pa$$w0rd" -AsPlainText -Force)

Configuring the password never expires setting:

Set-LocalUser -Name Peter -PasswordNeverExpires $true -Description Brother

Further thoughts

For administering Active Directory accounts I recommend my article PowerShell: My top 10 commands for documenting and monitoring Active Directory

Categories: PowerShell

Tagged as: ,

1 reply »

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.