SOLVED

Identify a running script in Get-Process and script PID

Brass Contributor

Hello!

1) While running a script with PowerShell 7.4, how to identify it from the output of Get-Process?

 

I tried to run for example C:\script\example_script.ps1 and, while the script was executing,

 

Get-Process | Select-String "example"

 

but this didn't output anything.

 

I also tried to insert this line in the script:

Write-Host "PID is: $PID"

 but this probably prints the PID of the PowerShell process where the script was launched.

 

2) How to print the script PID?

7 Replies

@Paige__Tanner 

1) You can use Get-Process | Where-Object ProcessName -eq 'Example' . Alternatively, you can use Get-Process | Where-Object ProcessName -Match 'Example'' to match everything containing Example.

2) Correct, that shows the PID of the PowerShell process. This will give you the process name and the PID

Get-Process | Where-Object ProcessName -eq 'Example' | Select-object Id, ProcessName



Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.

If one of the posts was helpful in other ways, please consider giving it a Like.

It was very helpful, thank you so much. But I have still a doubt: is it possible to print the script PID directly from the script code itself? If $PID is not the right variable, is there something else that can be used?
write-host $PID should be the thing to print/display when running a script and if you want to show it.

@Paige__Tanner 

 

Assuming this is on Windows, there's a more efficient way to fetch the current process:

 

$CurrentProcess = [System.Diagnostics.Process]::GetCurrentProcess();

 

Where you can then use the variable any way you see fit.

 

LainRobertson_0-1709074037240.png

 

It is safe to print process information - be that $pid or anything else - from within the script.

 

Cheers,

Lain

First of all, thank you.

Yes, all the tests are performed in Windows 11 and PowerShell 7.4.
I tried the suggested solutions from the answers and they all print the process ID of the parent PowerShell instance where the script is launched.
Also, I tried to run this script: C:\script\example_script.ps1

 

$currentProcess = [System.Diagnostics.Process]::GetCurrentProcess();
Write-Host "Hello from process $($currentProcess.Id); name: $($currentProcess.Name)"
$currentProcess.CommandLine
$currentProcess.CPU
$currentProcess.MachineName
$currentProcess.Parent
$currentProcess.Path
$currentProcess.ProcessName
Start-Sleep -Seconds 120

and this is the output:

PS C:\> .\script\example_script.ps1
Hello from process 16540; name: pwsh
"C:\Program Files\PowerShell\7\pwsh.exe"
5.25
.

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     44    73.57      63.66      30.89   15976   1 WindowsTerminal
C:\Program Files\PowerShell\7\pwsh.exe
pwsh

While the script was still in execution, I tried:

Get-Process | Select-String "example"
Get-Process | Where-Object ProcessName -Match "example"

but in either case there was no output.

I try to rephrase my doubt: if I run the script C:\script\example_script.ps1 from the PowerShell 7.4 command line, does that running script receive its own PID?
I ask because this is the case, for example, in Unix-like systems, with bash or other shells, and it can be shown in the list of processes.

Instead, here, both $PID or [System.Diagnostics.Process]::GetCurrentProcess() seem only to show the PID of the shell where the script is run, as if a running script was not related to a separate PID. Is this the case?

best response confirmed by Paige__Tanner (Brass Contributor)
Solution

@Paige__Tanner 

 

A script isn't a process. It's uncompiled code that runs inside of an interpreter - a term you won't hear often in this era.

 

So, there is no concept of a process id for a script, as the process - and therefore the process id - is implicitly that of the interpreter running the script.

 

It's no different from earlier scripting iterations like those from the Windows Scripting Host (such as JavaScript and VBScript), where in that case, the process id would have been that of the Windows Scripting Host (be that WScript.exe or CScript.exe).

 

Is there a particular challenge you're trying to solve or are you just curious about the topic?

 

Cheers,

Lain


@LainRobertson wrote:

@Paige__Tanner 

 

There is no concept of a process id for a script, as the process - and therefore the process id - is implicitly that of the interpreter running the script.

 

 


This is very clarifying.

 


Is there a particular challenge you're trying to solve or are you just curious about the topic?


No, it was just out of curiosity: as mentioned before, I looked for a similarity between bash (or ksh, or other Unix-like shells) and PowerShell. But they are different as regards this aspect.

I was also looking for this to obtain the status of the script execution, after the system exits from sleep mode.

1 best response

Accepted Solutions
best response confirmed by Paige__Tanner (Brass Contributor)
Solution

@Paige__Tanner 

 

A script isn't a process. It's uncompiled code that runs inside of an interpreter - a term you won't hear often in this era.

 

So, there is no concept of a process id for a script, as the process - and therefore the process id - is implicitly that of the interpreter running the script.

 

It's no different from earlier scripting iterations like those from the Windows Scripting Host (such as JavaScript and VBScript), where in that case, the process id would have been that of the Windows Scripting Host (be that WScript.exe or CScript.exe).

 

Is there a particular challenge you're trying to solve or are you just curious about the topic?

 

Cheers,

Lain

View solution in original post