Solved

powershell datetime parse help

Posted on 2014-09-22
6
286 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

717 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