Home > Enterprise >  get-localuser Passwordexpires in seconds (left from now)
get-localuser Passwordexpires in seconds (left from now)

Time:12-01

get-localuser |fl brings back the Date in that format: 

PasswordExpires        : 10/01/2022 15:31:34

how can i get this date counted in seconds from now? with powershell? can someone help me out?

AccountExpires         : 
Description            : 
Enabled                : True
FullName               : test-deleteme
PasswordChangeableDate : 29/11/2021 15:31:34
PasswordExpires        : 10/01/2022 15:31:34
UserMayChangePassword  : True
PasswordRequired       : True
PasswordLastSet        : 29/11/2021 15:31:34
LastLogon              : 
Name                   : test-deleteme
PrincipalSource        : Local
ObjectClass            : User

CodePudding user response:

The value of the PasswordExpired value does not in itself have a specific format - but PowerShell chooses a default format based on your locale once it need to output it to the screen.

If you want to calculate the duration from now until the date specified in PasswordExpiration, use the subtraction operator (-) inside a calculated property:

Get-LocalUser |Select-Object Name,@{Name='PasswordExpiresInSeconds'; Expression={$_.PasswordExpires - (Get-Date)}}

This will produce a [timespan] value describing the duration. To get just the number of seconds, reference the TotalSeconds property on the resulting timespan object:

Get-LocalUser |Select-Object Name,@{Name='PasswordExpiresInSeconds'; Expression={$($_.PasswordExpires - (Get-Date)).TotalSeconds -as [int]}}
  • Related