Solved

powershell datetime parse help

Posted on 2014-09-22
6
272 Views
Last Modified: 2014-09-24
Hello EE,

I have a a creation date in string format from gwmi win32_process , it returns a date format that is unusual , how do I parse this correctly, as the nano seconds after the seconds does not have a character format that I am aware of ?

20140913014133.145231+000

 {[datetime]::ParseExact($_.CreationDate,????? ,$null)}
0
Comment
Question by:davesnb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 16

Accepted Solution

by:
Dale Harris earned 400 total points
ID: 40336752
Does this work for you?

[DateTime]::ParseExact($_.CreationDate.substring(0,14),'yyyyMMddhhmmss',$null)
0
 
LVL 29

Assisted Solution

by:becraig
becraig earned 100 total points
ID: 40336852
You can also just split the date (though the substring should work as well)

 [DateTime]::ParseExact(($_creationDate.split(".")[0]), 'yyyyMMddHHmmss', $null)

Open in new window

0
 

Author Closing Comment

by:davesnb
ID: 40336874
Works great, thank you
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 16

Expert Comment

by:Dale Harris
ID: 40336881
You need to be careful not to use the HH since it needs to be in 12 hour format, not 24 in order for it to properly parse.  But yes, either way whether you split it or substring it should work just fine.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40336909
Usually that is a sufficient way to convert WMI Dates, but the "officially correct" way is to use this:
[ManagementDateTimeConverter]::ToDateTime($_.creationDate)

Open in new window

This allows for adjustment of time zone info, which can get important if you use a remote WMI query.
0
 
LVL 40

Expert Comment

by:footech
ID: 40341197
I know the question is already answered, but was just going through my email alerts.
The other way that I've always seen is to use the ConvertToDateTime method that is added to all WMI objects returned by PowerShell.  Quick example.
$a = gwmi win32_process -filter "name = 'svchost.exe'"
$a | % { $_.ConvertToDateTime($_.CreationDate) }

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question