0

I have created an array to store elements of Ids of a Get-Process request.

$arr = [int64[]]::new(1000);

Here is the code to output the data

$process = Get-Process | Where-Object {$_.handles -gt 1000}

enter image description here

I want to select the column for IDs specifically, and here is the code below

$a = $process | Select-Object -Property Id

enter image description here

My question now is how do you store the data for $a inside the $arr

3
  • 3
    $a = $process.ID gives you an array of process ID values. In a pipeline you can also use $process | Select-Object -ExpandProperty ID or $process | ForEach-Object ID.
    – zett42
    Commented Oct 11, 2024 at 12:39
  • 3
    .. and if you want these values to become Int64, just do $a = [int64[]]$process.Id. What would be the point of creating a thousand item array and then trying to insert values into that afterwards?
    – Theo
    Commented Oct 11, 2024 at 12:44
  • 2
    As one-liner: $a = [int64[]](Get-Process | Where-Object {$_.handles -gt 1000}).Id
    – Theo
    Commented Oct 11, 2024 at 14:53

1 Answer 1

3

Why do you want to create the array and populate it as two steps? You can just cast the result of the pipeline to int64[].

But Select-Object -property Id doesn't get you a value you can cast to int64, so that will never work. You can just use ForEach-Object { $_.Id } instead, however.

 $arr = [int64[]](Get-Process |
         Where-Object { $_.handles -gt 1000 } | 
         ForEach-Object  { $_.Id })
1
  • 2
    Or, more simply: [int64[]] (Get-Process | Where-Object Handles -gt 1000 | ForEach-Object Id or even [int64[]] (Get-Process | Where-Object Handles -gt 1000).Id or, also for better performance, [int64[]] (Get-Process).Where({ $_.Handles -gt 1000 }).Id
    – mklement0
    Commented Oct 11, 2024 at 19:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.