8

Is it possible to detect from within Powershell whether it is a nested shell?

If I open a Powershell or cmd.exe window, and then type powershell <enter> in there, is there a magic $host.somevariable I can query to find out if it is an "inner" shell?

1 Answer 1

9

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.ParentProcessId
if ($parent.ProcessName -eq 'powershell') {
    'nested, called from powershell'
}
elseif ($parent.ProcessName -eq 'cmd') {
    'nested, called from cmd'
}
else {
    'not nested'
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.