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: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s