Differentiate between console and ISE

Welcome Forums General PowerShell Q&A Differentiate between console and ISE

This topic contains 3 replies, has 4 voices, and was last updated by

 
Keymaster
4 years, 5 months ago.

  • Author
    Posts
  • #15499

    Participant
    Points: 0
    Rank: Member

    Hi
    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)
             Return
        }

    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?
    Thanks
    Tony

  • #15500

    Participant
    Points: 0
    Rank: Member

    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

    Keymaster
    Points: 1,619
    Helping HandTeam Member
    Rank: Community Hero

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

  • #114445

    Participant
    Points: 1
    Rank: Member

    You can do the check without errors:

     

    If (Test-Path Variable:PSise) {

    "This Runs in ISE"

    }

    Else {

    "This runs in Powershell or other console"

    }

     

The topic ‘Differentiate between console and ISE’ is closed to new replies.