Change value of a psobject property

TeemoBiceps picture TeemoBiceps · May 10, 2017 · Viewed 9.2k times · Source

I have an array full of psobjects. Now I want to change some properties in each of the objects in the array.

My code:

[array] $objectArray = $null
foreach ($row in $result) {
    $object = New-Object -TypeName PSObject
    $object | Add-Member -MemberType NoteProperty -Name "Classification" -Value $classification
    $object | Add-Member -MemberType NoteProperty -Name "Status" -Value $status
    $object | Add-Member -MemberType NoteProperty -Name "Priority" -Value $priority
    [array] $objectArray += $object
    Remove-Variable -Name object -ErrorAction SilentlyContinue
}

foreach ($co in $objectArray) {
    $oldStatus = $co.Status
    $oldPriority = $co.Priority
    $oldClassification = $co.Classification

    foreach ($uid in $resultUids.Tables[0]) {
        if ($oldStatus -eq $uid.status_i_d) {
            $co.Status = $uid.status_i_d
            Write-Host "status" $co.Status "uid" $uid.uid
        } elseif ($oldPriority -eq $uid.priority_i_d) {
            Write-Host "priority" $co.Priority "uid" $uid.uid
        } elseif($oldClassification -eq $uid.classification_i_d) {
            Write-Host "classification" $co.Classification "uid" $uid.uid
        }
    }
}

The code above works, but I just don't know how to change the value of the property itself. I would like to replace the value of "Classification", "Status" and "Priority" with $uid.uid.

Answer

gvee picture gvee · May 10, 2017

Get the value:

$co.Classification

Set the value:

$co.Classification = "some value"