Author Posts

May 21, 2014 at 11:59 pm

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

May 22, 2014 at 12:10 am

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"
}

May 22, 2014 at 5:51 am

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