Quantcast
Channel: Active questions tagged windows - Super User
Viewing all articles
Browse latest Browse all 9696

How do we know if a ps1 file is executed from CLI or GUI?

$
0
0

When a ps1 file is executed, how do we know that the file is executed from CLI command line or from GUI Explorer (the file itself or another ps1 file)?

There are several reasons I ask this. One thing is that we need pause to read the output, but we don't need it if execute from CLI.

For example, this file:

# Test.ps1#$FromCLI = if ($FromCLI) {'Executed From CLI'} else {'Executed From PS1 File; pause'}

What code is needed in $FromCLI=?So that we can differentiate between executed from CLI or GUI (ps1 file).


ChatGPT gives me this:

if ($host.Name -eq "ConsoleHost") {    Write-Host "Running from CLI"} elseif ($host.Name -eq "Windows PowerShell ISE") {    Write-Host "Running from GUI (Windows PowerShell ISE)"} else {    Write-Host "Running from GUI (Unknown)"}

The output always Running from CLI, both executed from CLI and its file.


Viewing all articles
Browse latest Browse all 9696

Trending Articles