Finding the process ID of the current powershell.exe instance

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Ondrej Zilinec Ondrej Zilinec 2 years, 2 months ago.

  • Author
    Posts
  • #18851
    Profile photo of Adam Bertram
    Adam Bertram
    Participant

    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
    Profile photo of Adam Bertram
    Adam Bertram
    Participant

    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
    Profile photo of Ondrej Zilinec
    Ondrej Zilinec
    Participant

    🙂 I like those self-resolving issues 🙂

You must be logged in to reply to this topic.