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.