PowerShell Get Current Console Process

Viewed 28

I would like to get the process ID or handle of the current powershell script when I double click and run a PS1 file from Windows Explorer.

This works:

$hwndPowerShell = Get-Process -Name "PowerShell"

But if I have multiple PowerShell console open, how do I get the handle of the current script?

I know one other way is to use the Win32 API FindWindow to find the latest instance of PowerShell console.

1 Answers

Try this command ,in commandline property it will list the script path which we can use to identity the script and PID.

Get-CimInstance -ClassName Win32_Process -filter "name = 'powershell.exe'" | Select-Object -property Name,CommandLine,HandleCount,ProcessID,WorkingSetSize,CreationDate

Related