Reputation: 57
I am using the dos command "w32tm" to convert an Active Directory LastLogonTimestamp into a readable date format. However it gives me something like this: 150215 02:40:10.0843593 - 11/04/2012 12:40:10 PM
How would I go about extracting just the date from the string? So I can have a variable with just "11/04/2012" in it.
Thanks.
Upvotes: 2
Views: 7704
Reputation: 126842
Here's another option (works for System.DirectoryServices.SearchResult objects)
# gets the current logged on user lastlogontimestamp
$user = ([ADSISEARCHER]"(samaccountname=$env:USERNAME)").FindOne()
[DateTime]::FromFileTime([Int64]::Parse($user.Properties.lastlogontimestamp))
Upvotes: 3
Reputation: 5701
You can try the following code. This isn't the cleanest but it works!
[DateTime]::Parse($string.Split('-')[1]).ToString("MM/dd/yyyy")
This splits your input string 150215 02:40:10.0843593 - 11/04/2012 12:40:10 PM
into the fragment after the -
, passes it into .NET's DateTime.Parse() function, and then finally outputs the date portion of it.
Upvotes: 1