Bios Date -> year only

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Emanuel Delvoye Emanuel Delvoye 2 years, 2 months ago.

  • Author
    Posts
  • #18985
    Profile photo of Emanuel Delvoye
    Emanuel Delvoye
    Participant

    I'm trying to get the year from the bios releasedate, The following command gives me an empty result:

    Get-CimInstance Win32_BIOS | Select-Object @{n="ReleaseDate";e={$_.ConvertToDateTime($_.ReleaseDate).year()}}

  • #18986
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    This will work – assuming the release date is populated

    £> Get-CimInstance -ClassName Win32_Bios | select ReleaseDate

    ReleaseDate
    ———–
    12/05/2014 01:00:00

    With the WMI cmdlets you had to convert the date – you also have a bug in your code as Year is a property not a method. So use this

    Get-WmiObject Win32_BIOS | Select-Object @{n="ReleaseDate";e={($_.ConvertToDateTime($_.ReleaseDate)).Year}}

    OR

    the CIM cmdlet returns the date in a string so you need to extract the year

    Get-CimInstance -ClassName Win32_Bios | select @{N='ReleaseYear'; E={(($_.ReleaseDate -split "/")[2] -split " ")[0]}}

    You can't easily assume date format – .NET needs dates in MM/DD/YYYY to convert string to date

  • #18987
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I'm pretty sure the Get-CimInstance cmdlet will return the property as a DateTime object, not a string. Does this work for you?

    (Get-CimInstance Win32_BIOS).ReleaseDate.Year
    
  • #18989
    Profile photo of Stephen Owen
    Stephen Owen
    Participant

    How about this?

    Get-CimInstance Win32_BIOS | Select-Object @{n="ReleaseDate";e={$_.ReleaseDate | select -expand Year}}

  • #19011
    Profile photo of Emanuel Delvoye
    Emanuel Delvoye
    Participant

    Thank you all for the replies. It all worked.. I ended up using the following:

    Get-CimInstance Win32_BIOS | Select-Object @{n="ReleaseDate";e={$_.ReleaseDate.year}}

You must be logged in to reply to this topic.