Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2017/06/Remove_PSObject_Property #280

Open
utterances-bot opened this issue Jun 16, 2023 · 1 comment
Open

2017/06/Remove_PSObject_Property #280

utterances-bot opened this issue Jun 16, 2023 · 1 comment

Comments

@utterances-bot
Copy link

Remove a PowerShell Object property - LazyWinAdmin

In this article we’ll see how to remove a property from a Powershell Object

https://lazywinadmin.github.io/2017/06/Remove_PSObject_Property.html

Copy link

Tell me, please, If I have a object inside another object, both with a arraylist structure, how do I remove one property?

Here is my exemple:
#How do I remove $register[$I].item.value

$register = [System.Collections.ArrayList]::new()

$register.Add( [PSCustomObject]@{ item = New-Object System.Collections.ArrayList } ) > $null

$register[0].item.Add([pscustomobject]@{attribute = "1st";type = "1st-Type";value = "1st-Value"}) > $null
$register[0].item.Add([pscustomobject]@{attribute = "2st";type = "2st-Type";value = "2st-Value"}) > $null
$register[0].item.Add([pscustomobject]@{attribute = "Add";type = "Add-Type";value = "Add-Value"}) > $null
$register[0].item.Add([pscustomobject]@{attribute = "4th";type = "4th-Type";value = "4th-Value"}) > $null
$register[0].item.Add([pscustomobject]@{attribute = "5th";type = "5th-Type";value = "5th-Value"}) > $null

$i = 0
$j = 0

$do_loop = $true

$register[$i].item[$j].attribute

$register[$i].item[($register[$i].item.count -1)].attribute

While loop

While ($j -ne $register[$i].item.count)
{
if ($register[$i].item[$j].attribute -eq "Add")
{
$register[$i].item.Add([pscustomobject]@{attribute = "3rd";type = "3rd-Type";value = "3rd-Value"}) > $null
}elseif ($register[$i].item[$j].attribute -eq "3rd")
{
$register[$i].item.Add([pscustomobject]@{attribute = "WORKED!";type = "WORKED!-Type";value = "WORKED!-Value"}) > $null
}elseif (($ffff -eq $nulo) -and $register[$i].item.count -eq 7)
{
$register[$i].item.Add([pscustomobject]@{attribute = "NULO!";type = "NULO!-Type";value = "NULO!-Value"}) > $null
}elseif ($register[$i].item.count -eq 8)
{
$register[$i].item.Remove($register[$i].item[7])
$register[$i].item.Add([pscustomobject]@{attribute = "NOTNULL!";type = "NOTNULL!-Type";value = "NOTNULL!-Value"}) > $null
}

Write-Host $register[$i].item[$j].attribute "=" $register[$i].item[($register[$i].item.count -1)].attribute

$j++

}

Write-Host ""
Write-Host "[5]" $register[$i].item[5]
Write-Host "[6]" $register[$i].item[6]
Write-Host "[7]" $register[$i].item[7]

$register[$i].item.Remove($register[$i].item[6])

Write-Host ""
Write-Host ""
Write-Host "[5]" $register[$i].item[5]
Write-Host "[6]" $register[$i].item[6]
Write-Host "[7]" $register[$i].item[7]

Write-Host ""
Write-Host "all:"
foreach ($for_each in $register[$i].item)
{
Write-Host $for_each.attribute $for_each.type $for_each.value
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants