How to get the process ID of the running process, as shown in the task manager

I am studying powershell and trying to understand how variables and functions can be used. I want to print the PID for all running notepad instances, basically what is shown in the PID column on the Details tab in the task manager. I wrote the following code

$cmd = {
  param($abc)
  Write-Host $abc
}

$processes = Get-Process -Name notepad | Select -ExpandProperty ID 
foreach ($process in $processes) 
{ 
    Start-Job -ScriptBlock $cmd -ArgumentList $process
}

I get the following result.

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
50     Job50           BackgroundJob   Running       True            localhost            ...                      
52     Job52           BackgroundJob   Running       True            localhost            ...                      

Two questions here. 1. I want only the PID, it has a lot.
2. I expect that the Id in the output is higher than the PID, but what is shown in the task manager is very different.

Can you tell me what I'm doing wrong?

+1
source share
2 answers

PID vs ID

PID, $processes, . , Start-Job PID.

2 notepad.exe, PowerShell, , 2 . 50 52 - , . , , .

script Get-Job | Receive-Job, PID. TechNet

Start-Job? script? Invoke-Command, scriptblock $cmd.

$cmd = {
  param($abc)
  Write-Host $abc
}

$processes = Get-Process -Name notepad | Select -ExpandProperty ID 
foreach ($process in $processes){ 
    Invoke-Command -ScriptBlock $cmd -ArgumentList $process
}

PowerShell 5.0, Write-Host . , Write-Output.

+4

pid :

 Get-Process -Name chrome | select Id | Format-List

Get-Job | select ID | Format-List

Id : 2
0

Source: https://habr.com/ru/post/1625855/


All Articles