Cole
Cole

Reputation: 183

Calculate AD password age from expiration date Powershell

I want to calculate the age of the password, in days, using the PasswordLastSet property. I've created the below code but Password Age never outputs anything. I'm guessing there is a type error but I'm not sure how to convert to DateTime. Any thoughts?

Get-ADComputer myComputer -Properties PasswordLastSet | Select-Object -Property PasswordLastSet, @{Name = 'Password Age';Expression = {Get-Date - ([datetime]($_.PasswordLastSet))}}

Upvotes: 0

Views: 3481

Answers (1)

Lee_Dailey
Lee_Dailey

Reputation: 7489

The reason for the failure is the lack of parens around the Get-Date call. Without those parens, the cmdlet tries to use the remainder of the line as input ... [grin] wrapping the cmdlet in parens - (Get-Date) - forces the cmdlet to run without trying to parse the remainder of the line.

Upvotes: 3

Related Questions