0

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.

  • @abshalom’s answer solves your problem, but see also https://stackoverflow.com/a/51178465/3156906 - it’s for an unrelated question but gives some good background info about both ```psobject``` and ```pacustomobject``` types - particularly this quote: “Note that - surprisingly - [pscustomobject] is the same as [psobject]: they both refer to type [System.Management.Automation.PSObject]” – mclayton Jun 05 '23 at 11:47
  • 1
    @mclayton, you might be generally right but in this case it concerns an `array` vs. a `PSCustomObject`, see: `$test.PSTypeNames` – iRon Jun 05 '23 at 12:36
  • As an aside: [try avoid using the increase assignment operator (`+=`) to create a collection](https://stackoverflow.com/a/60708579/1701026) as it is exponentially expensive. Besides, I recommend you to avoid using the [legacy approach](https://learn.microsoft.com/powershell/scripting/learn/deep-dives/everything-about-pscustomobject#legacy-approach) (using `New-Object` and `Add-Member`) to build a `[PSCustomObject]`. – iRon Jun 05 '23 at 12:36

1 Answers1

2

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                                                                                                                              
Avshalom
  • 8,657
  • 1
  • 25
  • 43
  • Although this will work, I recommend to use the [Array subexpression operator `@( )`](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_operators#array-subexpression-operator--) to force an array **afterwards** (*where the result is always an array of 0 or more objects.*): `$test_filtered = @($test | Where-Object {$_.nummer -match '175'})` rather than using it as a kind of initializer [for building a collection](https://stackoverflow.com/a/60708579/1701026). – iRon Jun 05 '23 at 12:44