Home > Software engineering >  Extract the PID from an array
Extract the PID from an array

Time:02-17

I am using the following command to sort the process based on Virtual Memory Utilization:

PS C:\Windows\system32> Get-Process | Sort PagedMemorySize64 -Desc | Select Id, Name, PagedMemorySize64 , VirtualMemorySize64 -First 10 |sls jqlserver

@{Id=37724; Name=jqlserver; PagedMemorySize64=3147898880; VirtualMemorySize64=27032002560}

My question how do I extract this the PID value 37724 to a variable $PID1. I tried so many things parsing arrays in Powershell, somehow I am unable to get it. Could you please provide me an idea?

Thank you

CodePudding user response:

Seems like you're only interested in the jqlserver process, in which case, you wouldn't have a need to use Select-String (sls), PowerShell cmdlets outputs objects in general and there is no need to parse them:

$process = Get-Process -Name jqlserver | Sort-Object PagedMemorySize64 -Descending |
           Select-Object Id, Name, PagedMemorySize64, VirtualMemorySize64 -First 10
$pid1 = $process[0].Id

$process[0] on above example, refers to the first element (Index 0 - see about_Arrays) of the array of objects (object[]) generated by Get-Process followed by .Id refers to the Id property and gets it's value.

See about_Properties for more useful information.

  • Related