PowerShell for Windows Admins

Aug 23 2010   12:39PM GMT

WMI date issue

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:

Working with WMI dates can be awkward sometimes. For instance if we look at the last boot up time of our system

$machine = Get-WmiObject -Class Win32_OperatingSystem

PS> $machine.LastBootUpTime
20100823183135.359600+060

We get a non-intuitive result

The ConvertToDateTime method can rescue us from this problem

PS> $machine.ConvertToDateTime($machine.LastBootUpTime)

23 August 2010 18:31:35

And we can even use it with write-host

PS> Write-Host $machine.ConvertToDateTime($machine.LastBootUpTime)
23/08/2010 18:31:35

However if we want to make the boot time part of a larger string we get a problem

PS> Write-Host “$machine.ConvertToDateTime($machine.LastBootUpTime)”
\\RSLAPTOP01\root\cimv2:Win32_OperatingSystem=@.ConvertToDateTime(\\RSLAPTOP01\root\cimv2:Win32_OperatingSystem=@.LastBootUpTime)

so we can use a sub-expression

PS> Write-Host “Boot up Time $($machine.ConvertToDateTime($machine.LastBootUpTime))”
Boot up Time 08/23/2010 18:31:35

but we end up with date that has changed format and is confusing if you are used to DD/MM/YYYY as we use in the UK. We can then use the DateTime property to get back to the format we need

PS> Write-Host ($machine.ConvertToDateTime($machine.LastBootUpTime)).DateTime
23 August 2010 18:31:35

PS> Write-Host “Boot up Time $($machine.ConvertToDateTime($machine.LastBootUpTime).DateTime)”
Boot up Time 23 August 2010 18:31:35

Done.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: