Link to home
Start Free TrialLog in
Avatar of nesher13
nesher13Flag for Israel

asked on

Converting result of the output to needed format

I get the output of the variable $WinInsDate in the format "20120509120504.000000 +180"

how can I convert it to a format similar to output command

 Get-Date -DisplayHint date ?
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
WMI dates don't have the additonal space between time and timezone offset (in minutes). Either the format above is wrong, or there is a different source ...

If you want to do it the hard, but most flexible way:
invoke-expression ( "20120509120504.000000 +180" -replace '^(\d{4})(\d{2})(\d{2})(\d\d)(\d\d)(\d\d)\.\d+ \+(\d+)', '(get-date -date "$2/$3/$1 $4:$5:$6").AddMinutes($7)')

Open in new window

I've ignored the milli/microseconds stuff.
Avatar of nesher13

ASKER

to footech

how can I convert the resulting format into a format mm/dd/yyyy ?
Both expressions will result in a datatime, which again can be formatted by e.g. stuffing into get-date -format .... or using the -f operator (which will automatically use the date format set up in the OS):
  get-date -format 'MM/dd/yyyy' $yourDateTime
   '{0:d}' -f $yourDateTime
I found a pretty simple solution to the second part of the question

PS C:\>$a = (Get-WmiObject Win32_OperatingSystem)

PS C:\>$a.converttodatetime($a.installdate)

Sunday, August 04, 2013 5:46:45 PM

PS C:\>$a.converttodatetime($a.installdate) | get-date -format d

8/4/2013
I've requested that this question be closed as follows:

Accepted answer: 0 points for nesher13's comment #a39520350
Assisted answer: 500 points for footech's comment #a39517538

for the following reason:

footech answer gave me the basis for solving the problem
footech answer gave me the basis for solving the problem
So your date literal "20120509120504.000000 +180" was wrong? The corresponding WMI date literal is "20120509120504.000000+180" (no space, as mentioned above).