Create new array from existing array data

This post was originally published on this site

I have an array, $bigList = @()

it contains for example

VMname PowerState Owner Status
MyServer1 PoweredOn J.Bloggs Installed
MyServer2 PoweredOff M.Smythe Decommissioned
MyServer3 PoweredOff M.Smythe Installed


I want to create a second array if PowerState=PoweredOff and Status=Decommissioned

The second Array contents will be output as a .CSV


What i’ve come up with is


$smallList = @()

foreach ($row in $bigList){

     if(($bigList.PowerState -match ‘PoweredOff’) and ($bigList.Status -match ‘Decommissioned)){

          $smallListProperty = [ordered] @{

               ‘VM Name’ = $bigList.VMname

               ‘Owner’ = $bigList.Owner


     $smallList += new-object -TypeName psobject -Property $smallListProperty


$smallList | export-CSV -path c:TempsmallList.csv


It kind of works but the output i’m getting is

VM Name Owner
System.Object[] System.Object[]



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.