powershell where-object changes type of psobject to pscustomobject - how to workaround?

228 Views Asked by At

Example Code:

$test=@()       
$new = New-Object PSObject
$new | Add-Member -type NoteProperty -name name -Value 'test'
$new | Add-Member -type NoteProperty -name nummer -Value "17580-10"
$new | Add-Member -type NoteProperty -name datum -Value "10.08.23"
$test += $new
$test

$test_filtered=@()
$test_filtered=$test | Where-Object {($_.nummer -match '175')}

Output:

PS C:\Users\cm> $test.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                           
-------- -------- ----                                     --------                                                                                                                           
True     True     Object[]                                 System.Array                                                                                                                       


PS C:\Users\cm> $test_filtered.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                           
-------- -------- ----                                     --------                                                                                                                           
True     False    PSCustomObject                           System.Object

As you can see, the type get's changed. I need to keep the Object as it is (before). How to workaround that?

Thanks for any idea.

1

There are 1 best solutions below

1
Avshalom On BEST ANSWER

You're overwriting the Array object with a psobject (the Where-Object return a single item), if you want to keep the Array type, Add the filtered data results into the array. so change this line:

$test_filtered = $test | Where-Object {($_.nummer -match '175')}

to:

$test_filtered += ($test | Where-Object {($_.nummer -match '175')})

PS C:\> $test.GetType()
    
IsPublic IsSerial Name                                     BaseType                                                                                                                                  
-------- -------- ----                                     --------                                                                                                                                  
True     True     Object[]                                 System.Array                                                                                                                              
   

PS C:\> $test_filtered.GetType()
    
IsPublic IsSerial Name                                     BaseType                                                                                                                                  
-------- -------- ----                                     --------                                                                                                                                  
True     True     Object[]                                 System.Array