Differentiate between console and ISE

I’ve cobbled together a number of code snippets to create a script that copies an ADUser (ADUC > Right Click User > Copy) including the stuff AD misses out (Streetname etc). I’m quite happy with this so far and am trying to prettyify it.

I use ISE by default, but it may also be run by others from the console. I am wanting to put a ‘Press any key…’ at the end of the script to say that it’s done and have used:
$x= $Host.UI.RawUI.ReadKey(“NoEcho, IncludeKeyDown”)
as suggested in Don Jones on this site.

However this throws an error in ISE (hate - red text). I’ve since found some code that uses $psISE to check if it’s in ISE and then run some code.

    If (!$psISE) {
         The "ReadKey" functionality is not supported in Windows PowerShell ISE.
        $Shell = New-Object -ComObject "WScript.Shell"
        $Button = $Shell.Popup("Click OK to continue.", 0, "Script Paused", 0)

However, this throws errors in the console.

Down to my question: is there a way that I can tell if the code is being run from the console or ISE?

You could use the command Pause to get the effect you want. It works in both ISE and Console mode.

Else there is the much less efficient, and not necessarily future-safe way of doing it:

if($host.Name -eq ‘ConsoleHost’) {
“this is in a console”
} elseif($host.Name -eq ‘Windows PowerShell ISE Host’) {
“this is in ISE”
} else {
“none of the above”

You can check properties of $host to see what you’re running in.

You can do the check without errors:


If (Test-Path Variable:PSise) {

“This Runs in ISE”


Else {

“This runs in Powershell or other console”