Mr I
Mr I

Reputation: 25

PowerShell regular expression 14 digits

I have a regular expression I'm trying to get working to match the LastLogon property in the Win32_NetworkLoginProfile object. So far I have the following:

Get-WmiObject Win32_NetworkLoginProfile |
    Sort -Descending LastLogon |
    Select * -First 1 |
    where { $_.LastLogon -match "(\d{14})" }

However, this doesn't seem to match the 14 digit output of the attribute in the object. I think it could be a error with the regular expression I'm using but I cant for the life of me work it out.

Upvotes: 0

Views: 182

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

If you want just one attribute you need to select just that attribute (or expand it if you want just the value), otherwise PowerShell will display the default display property set of the object. To extract the date and time portion before the decimal point simply remove the decimal point and everything after it.

Get-WmiObject Win32_NetworkLoginProfile |
    Sort -Descending LastLogon |
    Select-Object -Expand LastLogon -First 1 |
    ForEach-Object { $_ -replace '\..*' }

Upvotes: 1

Related Questions