0

I have the following code:

$array = Get-Process

Now, I want to count the number of columns. I have tried:

$array.Columns.Count
$array.Columns

And all without luck.

What should I do to get the correct number of columns? That should be 8.

<<Added on January, 27th, 2021: >>

I have modified the code to:

$Processes = @(Get-Process | Select-Object -Property Name, Description, ProductVersion, @{Name="Application";Expression={$_.Description + " " + $_.ProductVersion}},@{Name="Executable";Expression={(($_."Path").split("\"))[-1]}} |Where {$_.Executable})
$Array     = @()
$Record    = [ordered] @{"Name"         = "";
                         "Application"  = ""}

ForEach ($ProcesName in $Processes)
{
 $Record."Name"        = $ProcesName.Name
 $Record."Application" = $ProcesName.Application
 $objRecord            = New-Object PSObject -Property $Record
 $Array               += $objRecord
}
Clear-Host
Write-Host "Number of columns: $($Array.Column.Count)"
Write-Host "Number rows:       $($Array.Count)"

The result is the same: both the column and row are the same. And that is not correct. I want to use the technique in a new script with much more columns.

<<End part that has been added on January, 27th, 2021 >>

Feedback is appreciated.

With kind regards, TheStingPilot

1
  • 1
    What you see in the display is a default view for that particular object type. The actual number of properties is much greater. I don't know the point of this exercise. Commented Jan 26, 2021 at 22:01

1 Answer 1

1

$array is an array of objects of type System.Diagnostics.Process. There aren't columns as such. The objects have properties which you may choose to display in columns using $array | ft or as a list $array | fl *. You can count the properties like this:

$array | Get-Member -MemberType Property | measure

If you'd like the answer to be 8 then you can do this:

$array | select Handles, NPM, PM, WS, CPU, Id, SI, Name | Get-Member -MemberType NoteProperty | measure
Sign up to request clarification or add additional context in comments.

2 Comments

Hello Mark, thanks for your feedback. I thought it was a simple question... Not. I have modified the question, see the part that has been added today.
I just had a brainwave: ($Array | Get-Member -MemberType NoteProperty | measure).Count

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.