Solved

powershell datetime parse help

Posted on 2014-09-22
6
280 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
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 70

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
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…

738 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