Home > Mobile >  Format-List or Format-Table with property of multiple variables
Format-List or Format-Table with property of multiple variables

Time:11-19

If I have multiple variables and I want to pull certain properties from all of them in a single view (for instance their Count), how would one do this?

For example, if I want something like the following

# Table Format
Name               Count
Variable1          $Variable1.Count
Variable2          $Variable2.Count
Variable3          $Variable3.Count

# List Format
Name      : Variable1
Count     : $Variable1.Count

Name      : Variable2
Count     : $Variable2.Count

Name      : Variable3
Count     : $Variable3.Count

# Variables are ArrayLists, hence the Count property

I thought this would be fairly trivial using the standard select-object, format-list or format-table cmdlets and use of calculated properties, but I just cannot get it to work as expected.

This was my first thought:

Format-List @{N='Variable1';E={$Variable1.Count}}, @{N='Variable2';E={$Variable2.Count}}

I guess those cmdlets cannot be called without piping something to them first, so then I tried the following, and it did what I wanted, however it seems to keep looping endlessly, outputting the results over and over and over.

@($Variable1, $Variable2) | Format-List @{N='Variable1';E={$Variable1.Count}}, @{N='Variable2';E={$Variable2.Count}}

Is there something stupid/simple I'm overlooking here?

CodePudding user response:

In a dynamic sense of approach, Get-Variable could be a good option if I'm understanding you correctly. This passes over the name, and the value which you'd be able to use a calculated property to convert over to the values sum/count:

$variable1 = 1..10
$variable2 = 5..15
$variable3 = 10..20
Get-Variable -Name variable1,variable2,variable3 | 
    Format-List -Property Name, @{
        Name = 'Count'
        Expression = { $_.Value.Count }
    }

With Format-List it will output:

Name  : variable1
Count : 10

Name  : variable2
Count : 11

Name  : variable3
Count : 11

...and with Format-Table:

Name      Count
----      -----
variable1    10
variable2    11
variable3    11

CodePudding user response:

Here is a generic method. I create a table with random names like Variable25. the table contains 10000 rows with the name having number between 1 and 100. Then Group by name to get counts

$table = [System.Collections.ArrayList]::new()
for($i = 0; $i -le 10000; $i  )
{
   $newRow = New-Object -TypeName psobject
   $number = Get-Random -Minimum 1 -Maximum 100
   $newRow | Add-Member -NotePropertyName Name -NotePropertyValue ("Variable"   $number)
   $table.Add($newRow) | Out-Null 
}
$groups = $table | Sort-Object -Property Name | Group-Object {$_.Name}
$tableCount = [System.Collections.ArrayList]::new()
foreach($group in $groups)
{
   $newRow = New-Object -TypeName psobject
   $newRow | Add-Member -NotePropertyName Name -NotePropertyValue $group.Name
   $newRow | Add-Member -NotePropertyName Count -NotePropertyValue $group.Count
   $tableCount.Add($newRow) | Out-Null 

}
$tableCount | Format-Table
  • Related