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 LICENSE NUMBERS, ACTIVATION 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.
This topic is 4 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
mail100
Posts: 2
Last visit: Sun Feb 06, 2022 1:34 pm

How to detect if a script is running inside PowerShell Studio

Post by mail100 »

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: 1654
Last visit: Wed Mar 27, 2024 1:54 pm
Answers: 39
Been upvoted: 30 times

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

Post by brittneyr »

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
SAPIEN Technologies, Inc.
User avatar
brittneyr
Site Admin
Posts: 1654
Last visit: Wed Mar 27, 2024 1:54 pm
Answers: 39
Been upvoted: 30 times

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

Post by brittneyr »

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
SAPIEN Technologies, Inc.
mail100
Posts: 2
Last visit: Sun Feb 06, 2022 1:34 pm

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

Post by mail100 »

Perfect, thanks for the quick reply. - Sean
This topic is 4 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.