How to detect Powershell nesting within Powershell? How to detect Powershell nesting within Powershell? powershell powershell

How to detect Powershell nesting within Powershell?


There is no such a magic variable, more likely. But it is possible to get this information:

$me = Get-WmiObject -Query "select * from Win32_Process where Handle=$pid"$parent = Get-Process -Id $me.ParentProcessIdif ($parent.ProcessName -eq 'powershell') {    'nested, called from powershell'}elseif ($parent.ProcessName -eq 'cmd') {    'nested, called from cmd'}else {    'not nested'}