How to detect if a script is running inside PowerShell Studio

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
mail100
Posts: 2
Joined: Mon Aug 20, 2018 12:26 am

How to detect if a script is running inside PowerShell Studio

Post by mail100 » Wed Aug 14, 2019 6:55 pm

Product, version and build: 5.6.163
32 or 64 bit version of product: x64
Operating system: Win10 1809
32 or 64 bit OS: x64

I would like to detect if I am running my script under PowerShell Studio. With PowerShell ISE I could check $psISE for null, is there an equivalent for PowerShell Studio? Thanks.

User avatar
brittneyr
Site Admin
Posts: 129
Joined: Thu Jun 01, 2017 7:20 am

Re: How to detect if a script is running inside PowerShell Studio

Post by brittneyr » Thu Aug 15, 2019 7:36 am

The equivalent of $psISE for PowerShell Studio and PrimalScript is $SAPIENHost. If $SAPIENHost doesn't exist, then it is not running in either of the applications.
Brittney Ryn
SAPIEN Technologies, Inc.

User avatar
brittneyr
Site Admin
Posts: 129
Joined: Thu Jun 01, 2017 7:20 am

Re: How to detect if a script is running inside PowerShell Studio

Post by brittneyr » Thu Aug 15, 2019 8:39 am

Another way to determine what application your script is running under is to use $Host.Name.
If the script is running under PowerShell Studio or PrimalScript, it will return 'PrimalScriptHostImplementation'. If the script is running under the ISE, it will return 'Windows PowerShell ISE Host'.

Code: Select all

switch ($Host.Name){
    ConsoleHost { }
    PrimalScriptHostImplementation { }
    'Windows PowerShell ISE Host' { }
}
Brittney Ryn
SAPIEN Technologies, Inc.

mail100
Posts: 2
Joined: Mon Aug 20, 2018 12:26 am

Re: How to detect if a script is running inside PowerShell Studio

Post by mail100 » Thu Aug 15, 2019 11:45 pm

Perfect, thanks for the quick reply. - Sean