Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell - get process ID of called application

I need to call an external application (i.e. & 'Notepad' ) and then get the process ID of the called application.

Get-Process Notepad = will return all Notepad processes

I want to do something like:

$objApp = & 'c:\Notepad.exe'  WHILE (get-process -ID $objApp.id | select -property Responding) {   Start-Sleep -s 10   Echo "STILL WAITING" } Echo "Done!!" 
like image 706
Schlauge Avatar asked Jan 21 '11 19:01

Schlauge


People also ask

How do I find the process ID in PowerShell?

To find the PID of a process, type Get-Process . Indicates that the UserName value of the Process object is returned with results of the command. Specifies one or more process objects. Enter a variable that contains the objects, or type a command or expression that gets the objects.

How do I find the PID of a program?

Task Manager can be opened in a number of ways, but the simplest is to select Ctrl+Alt+Delete, and then select Task Manager. In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column.

What is $PID in PowerShell?

$PID is an automatic variable and contains the process identifier of the process hosting the current PowerShell session. With the -id option of get-process , it is possible to query some data about the current powershell process: get-process -id $PID | format-table startTime, path, workingSet.


1 Answers

Use Start-Process with the -PassThru argument like this:

$app = Start-Process notepad -passthru Wait-Process $app.Id 
like image 144
zdan Avatar answered Oct 03 '22 22:10

zdan