TOGEEK
TOGEEK

Reputation: 741

Change the WMI Date to DATE ONLY

I have the PS line that determines the date and time of the last reboot of a server. This works as intended, but because some of our servers are in US Locale, I wish to change the format such that it is in "MMM dd yyyy". I actually do not need the time so this will be handy.

$Lastrebootime = Get-WmiObject Win32_OperatingSystem -Computername $Computer |
                 select csname, @{LABEL='LastBootUpTime';EXPRESSION={
                   $_.ConvertToDateTime($_.lastbootuptime)
                 }} -ErrorAction Stop

There doesn't seem to be a ConvertToDate only?

Upvotes: 1

Views: 1169

Answers (1)

Joey
Joey

Reputation: 354794

The DateTime object has a property Date which contains only the date portion:

$_.ConverttoDateTime($_.lastbootuptime).Date

Otherwise if you want a specific format anyway, just format the date:

$_.ConverttoDateTime($_.lastbootuptime).ToString('MMM dd yyyy')

This doesn't care about the time portion because it doesn't even appear in the format string.

Upvotes: 4

Related Questions