Add Member to ManagementObject

Tagged: 

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Manuel Berfelde Manuel Berfelde 1 week, 3 days ago.

  • Author
    Posts
  • #70579
    Profile photo of Manuel Berfelde
    Manuel Berfelde
    Participant

    How can I add a member to an object returned from get-wmiobject?

    I expected this to work

    $results.add("BIOS",$((Get-WmiObject win32_BIOS | Add-Member -MemberType NoteProperty -Name "myDate" -Value (get-date))))

    but $results.bios is completly empty.

  • #70583
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Add-Member doesn't return anything, so the pipeline ends. Add-Member updates\modifies the object. You can see that myDate does exist in this resultset:

    $bios = Get-WmiObject win32_BIOS;
    $bios | Add-Member -MemberType NoteProperty -Name "myDate" -Value (get-date);
    $bios | Select *
    

    Adding the date could be accomplished with a calculated property:

    Get-WmiObject -Class Win32_BIOS | Select *, @{Name="myDate";Expression={Get-Date}}
    

    If you only need the SerialNumber and not all properties, you can also speed up and clean up your query like so:

    Get-WmiObject -Class Win32_BIOS -Property SerialNumber | Select SerialNumber, @{Name="myDate";Expression={Get-Date}}
    

You must be logged in to reply to this topic.