PowerShell ConvertToDateTime

PowerShell Converts a Date to a String with ConvertToDateTime

This page solves the real problem of displaying the boot time.  What it does is convert a hard-to-read number (20121006065349.125599+060) into a meaningful date string (06 October 2012).

Topics for PowerShell ConvertToDateTime

 ♣

Locate the Correct WMI Class

ConvertToDateTime is a ScriptMethod, we need a vehicle such as the time the computer started in order to apply this PowerShell technique.  I have chosen  LastBootUpTime, this property is likely to belong to a WMI operating system class and this is how I discovered it:

Get-WmiObject -List | Where-Object {$_.name -Match "OperatingSystem"}

Name
—-
CIM_OperatingSystem
Win32_OperatingSystem
Win32_OperatingSystemAutochkSetting
Win32_SystemOperatingSystem

Research Win32_OperatingSystem Properties

The key PowerShell cmdlet for getting to know the properties of other cmdlets is Get-Member, or GM for short.

Clear-Host
Get-WmiObject Win32_OperatingSystem | Get-Member

Digressing!
You can refine the output with the -MemberType parameter, and filter out those properties beginning with _underscore by appending [a-z]*.

Clear-Host
Get-WmiObject Win32_OperatingSystem | Get-Member -MemberType Property [a-z]*

Result: LastBootUpTime (For more precise confirmation substitute  -MemberType Property *boot*)

Reveal the LastBootUpTime Property

Problem 1: This simple command does not reveal the time of LastBootUpTime:
Get-WmiObject Win32_OperatingSystem

Problem 2: This technique reveals the Last Boot Time, but I cannot easily understand the format:

Clear-Host
(Get-WmiObject Win32_OperatingSystem).LastBootUpTime

Result: 20121006065349.125599+060

Solution: Master the ScriptMethod ConvertToDateTime

Before we apply Get-Member to a new situation, we need to research methods that convert the number into a readable date / time string.  Key point ConvertToDateTime is ScriptMethod (not a plain method).

Clear-Host
Get-WmiObject Win32_OperatingSystem | Get-Member -MemberType *method

Note 1: The wildcard * makes sure you see ScriptMethods

The Final Solution to Display LastBootUpTime

Clear-Host
$Boot = Get-WmiObject Win32_OperatingSystem
$Boot.ConvertToDateTime($Boot.LastBootUpTime)

Result: 06 October 2012 (I am in the UK)

Note 2: You need to introduce ConvertToDateTime with $Boot.  My point is that you really do require two $Boot. instructions on the last line. 

See how to calculate uptime since the last reboot.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Get-Date CSV Format

Here is an example which employs another PowerShell cmdlet called ConvertTo-Csv to control the date format.

Clear-Host
$DateCsv = Get-Date
ConvertTo-Csv -Inputobject $DateCsv -NoTypeinformation

Note 3: You could change the separator from a comma to a semi-colon by appending this parameter: -Delimiter ";"

See more on ConvertTo-Csv »

Summary of PowerShell ConvertToDateTime

Computing dates are never easy.  This page solves the real problem of displaying the boot time, by converting a hard-to-read number into a meaningful date string.

If you like this page then please share it with your friends

 


See more PowerShell examples for syntax advice

PowerShell Tutorials   • Syntax   • Get-Verb   • PowerShell Nouns   • Get-Credential

PowerShell -as   • Comparison operators  • Conditional operators   • Real-time Bandwidth Monitor

Get-Date  • Quotes   • Windows PowerShell   • PowerShell Version Check   • Get-Member

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.