Author Posts

July 9, 2015 at 9:59 am

So i'll briefly describe my issue and how i got it working, but not sure how !

Issue:
From the WMI command, Get-CimInstance -Namespace root\cimv2 -ClassName Win32_Product, when query the app and the install date i found the string that came back was written as 20150706.
I want to see the date displayed as 06/07/2015

Solution:
I needed to create a "white Space" between the year, month and date.

"20150706" -replace  ("([4-9]+)",'$1 ')

Result now is 2015 07 06

Now with the white space, i can split up the date separately
.Split(' ')
So added to the regex line to create white space

("([4-9]+)",'$1 ')).Split(' ')

Finally i need to rejoin the date but in the reverse order and with '/'
[2..0] -join "/"
So i'm joining the split up date in reverse order (2 to 0, backwards), then joining with '/' where the white space is.

So finally looks like this with the WMI query set as a variable:

$Product = Get-CimInstance -Namespace root\cimv2 -ClassName Win32_Product

$Product | select name,version, @{n='Date of Install';e={($_.installdate -replace ("([4-9]+)",'$1 ')).Split(' ')[2..0] -join "/"}}

Now, if i play a bit more i find this '"([4-5])",'$1 " does the same as this, ([4-9]+)",'$1 ', but without 9 (replaced with a 5) and no '+' required.

I was playing with this for a large amount of time and i'm not sure why what i did worked or fully understand. The [4-5], so this i don't fully get. the $1 i'm think is the variable is written too. Some basic Regex i get, ^start, $end. But pieces it together is very confusing !

Any help would be great.

July 9, 2015 at 10:11 am

In a regex, [4-9] will match any single character that's a 4,5,6,7,8 or 9.

I would make two suggestions here: one, don't use Win32_Product. Seriously, trust me on this. See http://myitforum.com/cs2/blogs/gramsey/archive/2011/01/25/win32-product-is-evil.aspx for explanation. 🙂

Two, rather than parsing this string yourself, it's probably easier to just use the DateTime class, which allows you to do all sorts of conversions to and from string. In this case:

$wmiDateString = '20150706'
$dateTime = [datetime]::ParseExact($wmiDateString, 'yyyyMMdd', [cultureinfo]::CurrentCulture)
$newString = $dateTime.ToString('dd/MM/yyyy')

In this case, I'm using custom date/time formatting strings from the .NET framework, which you can find documented here: https://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx

July 9, 2015 at 10:20 am

I did try looking into that, but didn't get far. Looking at your code i can see why ! its brilliant, would really like to have that level of understanding. Thanks.

July 10, 2015 at 3:38 pm

A minor tweak to Dave's great solution...

The current culture is what you would want to use almost all the time with this method, and the developers anticipated that and made it the default value for that parameter. So we can shorten it a bit and take out a confusing obscure reference thus:

$wmiDateString = '20150706'
$dateTime = [datetime]::ParseExact($wmiDateString, 'yyyyMMdd', $Null)
$newString = $dateTime.ToString('dd/MM/yyyy')

And especially now that it's simpler, I would tend to combine the two lines.

$wmiDateString = '20150706'
$newString = [datetime]::ParseExact($wmiDateString, 'yyyyMMdd', $Null).ToString('dd/MM/yyyy')

July 11, 2015 at 4:32 am

Thanks Tim. Can I ask, how did you find out about this and what part the $null is playing ?

Just for my learning and understanding.

July 13, 2015 at 5:33 am

Graham,

The first place to go for understanding a particular .Net object is always MSDN, which is the official Microsoft repository of such information. (It's not a great place for learning .Net, because it tends to assumes a high level of .Net experience in the reader, but it's the place to go for the intricate details of an object.) So I Googled "MSDN datetime ParseExact" to find the MSDN article on the ParseExact method for datetime objects.
https://msdn.microsoft.com/en-us/library/System.DateTime.ParseExact(v=vs.110).aspx

I saw there that ParseExact has 3 "overloads", so called because the method is overloaded with three different definitions. Overloads are distinguished by the number and type of parameters they take. Dave is using three parameters, so the first overload in the article is the one being triggered.
https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx

The three parameters are the string to be parsed, a string showing the format of the string to be parsed, and IFormatProvider. The third one is the complex one here of course. As the article explains, it can be any of several object types. The purpose of it is basically to tell .Net how you like to format your numbers, where you stick your commas and periods, whether you put your dates in US or European order, etc.

Fortunately, we don't have to understand it too deeply, because we can just tell it to use the same settings that current Windows session is already using. That's what Dave did.

Even better, at the end of the remarks, it says that it will also do that if we just use $Null instead, which is what I did. That makes it easier to remember and use, and easier for the person modifying your script in the future to understand what it does.

July 13, 2015 at 10:44 am

Thank you Tim for taking the time to reply. Brilliant reply.