This article describes how to get the PID of the current PowerShell process in PowerShell and then use it to do something interesting.
Have you ever tried to enter "$pid" in the PowerShell command line window? Try.
Copy Code code as follows:
PS c:\users\zhanghong> $pid
4404
For PID, we seem to be not unfamiliar. Yes, it is the process ID of a process. Whose process ID is the "$pid" here? Is the process ID of the PowerShell process for the current PowerShell this command line window. Note that the small series to open a Cmd.exe, and then in the cmd window input powershell into the PowerShell environment. So, here's the PID, not the Cmd.exe process ID, but the Powershell.exe process ID.
Well, after figuring out what this "$pid" is, let's take a look at what it does. Obviously, we can get to the current PowerShell process object through this "$pid". Then look at what time it started and kill the process.
First show the current time, and then see how long the process started.
PS c:\users\zhanghong> Get-date
September 18, 2013 9:05:07
PS c:\users\zhanghong> (Get-process-pid $pid). starttime
September 18, 2013 9:03:34
Kill it, and notice the change in the command prompt. has been returned to the Cmd.exe environment by the PowerShell environment. The kill here is Stop-process this cmdlet.
Copy Code code as follows:
PS c:\users\zhanghong> Kill $pid
C:\users\zhanghong>
Okay, about PowerShell. Get the current command line window process PID, small series on the introduction of so many, I hope to help you, thank you!