Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks for any idea.

>Solution :

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                                                                                                                              
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading