1

I would like to get the PID of my powershell script. I'am able to do that in bash like that :

#!/bin/bash

VARIABLE=$$

echo "This is a test"

echo $VARIABLE

The output is :

root@DESKTOP-TURGKNS:~# ./test.sh
THIS IS A VARIABLE
218

And if I execute the script again, the PID change every time.

In powershell, if I try that :

$PID

Write-Output "THIS IS A TEST"

The output is :

PS C:\Windows\system32> $PID

Write-Output "THIS IS A TEST"
5520
THIS IS A TEST

PS C:\Windows\system32> $PID

Write-Output "THIS IS A TEST"
5520
THIS IS A TEST

PS C:\Windows\system32> $PID

Write-Output "THIS IS A TEST"
5520
THIS IS A TEST

I think that $$ and $PID don't work in the same way.

There is someone to show me how to do that ?

aynber
  • 22,380
  • 8
  • 50
  • 63
Morph59
  • 33
  • 3
  • According to your posting, I would conclude that the process id of your powershell is 5520. What's wrong with it? Aside from this: Does [this](https://stackoverflow.com/questions/40771124/get-process-id-of-powershell-process) help, in particular the comment to the answer? – user1934428 Sep 06 '22 at 14:01

1 Answers1

4

Unlike shell scripts written for POSIX-compatible shells such as bash, PowerShell scripts (*.ps1 files) run in-process.

Therefore, all invocations of a given script (more generallly, all scripts) in a given PowerShell session (process) report the same value in the automatic $PID variable, namely the current process' ID.

To run a .ps1 script out-of-process, you'll have to call the PowerShell CLI (powershell.exe for Windows PowerShell, pwsh for PowerShell (Core) 7+), which creates a PowerShell child process; e.g.:

# Note: Passing a command via { ... } only works from *inside* PowerShell.
pwsh -NoProfile { ./some.ps1 }

# With arguments
pwsh -NoProfile { ./some.ps1 @args } -args foo, $PID

However:

  • PowerShell's startup cost is significant, so you pay a noticeable performance penalty.

  • Behind the scenes, XML-based serialization and deserialization is involved for communicating data types, and the type fidelity has limits, just as in PowerShell remoting. That is, if complex objects are passed to or received from the child process, you may only get emulations of these objects - see this answer for background information.

Note that if you're calling from outside PowerShell, use the CLI's -File parameter to invoke a script (in which case only text in- and output is supported); e.g.:

pwsh -NoProfile -File ./some.ps1 foo $PID

For a comprehensive description of the PowerShell CLI, see this answer.


I think that $$ [in bash] and $PID [in PowerShell] don't work in the same way.

They do: both report the current process' ID; the difference in observed behavior is solely due to the difference between execution in a child process vs. in-process execution.

As an aside: PowerShell too has an automatic $$ variable, but it serves an entirely different purpose than in bash (where it is the equivalent of PowerShell's $PID): It contains the last token of the most recently submitted command line and is intended for interactive editing convenience (e.g., after submitting Get-ChildItem someReallyLongDirectoryName, you can refer to someReallyLongDirectoryName with $$ at the next prompt).

As such, it is the equivalent of bash's built-in $_ variable.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Hello, your command `pwsh -NoProfile { ./some.ps1 }` do exactly what I search. Thanks for that. There is no way to put this in a Scheduled task ? I try to add `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe` in the Program/script section and `{ D:\Path\to\my\script\my_script.ps1 }` in the Add argument section from the settings part of my scheduled task but it won't work. Do you have any idea ? – Morph59 Sep 06 '22 at 17:38
  • @Morph59, a scheduled task by definition calls PowerShell _from the outside_, so you need a different syntax for the CLI call: `powershell.exe -NoProfile -File D:\Path\to\my\script\my_script.ps1` For a comprehensive description of PowerShell's CLI, see [this answer](https://stackoverflow.com/a/68136129/45375). – mklement0 Sep 06 '22 at 17:42