Differentiate between console and ISE

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Don Jones Don Jones 3 years, 1 month ago.

  • Author
  • #15499
    Profile photo of Tony Wainwright
    Tony Wainwright

    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?

  • #15500
    Profile photo of Martin Nielsen
    Martin Nielsen

    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"

  • #15503
    Profile photo of Don Jones
    Don Jones

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

You must be logged in to reply to this topic.