Finding the process ID of the current powershell.exe instance

Welcome Forums General PowerShell Q&A Finding the process ID of the current powershell.exe instance

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
4 years, 2 months ago.

  • Author
    Posts
  • #18851

    Participant
    Points: 22
    Rank: Member

    I'm running a script that kicks off many different processes. From what I can see, all of the external process that my script kicks off have a parent process ID of the powershell.exe instance they came from. For some reason, an insane software installer starts it's standard setup.exe, terminates, waits a few seconds and spawns another setup.exe process. Because of this, my -Wait param on Start-Process won't work and neither will Wait-Process. I did find that every process that setup.exe spawns has a parent process ID of the powershell.exe instance it's running under.

    I can get the powershell.exe process ID directly by just finding all IDs with a name of powershell.exe but I don't want to do that. I want to prevent a problem if another powershell.exe instance is running at the same time and my script just hanging forever. So...

    Anyone know of a way to get the process ID for powershell.exe from a running script?

  • #18852

    Participant
    Points: 22
    Rank: Member

    I never realized it'd be this simple. There's a variable of $pid that represents the current powershell instance I can use. Now that makes me wonder if I've ever used that reserved variable and not thought about it.

  • #18861

    Participant
    Points: 0
    Rank: Member

    🙂 I like those self-resolving issues 🙂

The topic ‘Finding the process ID of the current powershell.exe instance’ is closed to new replies.