# Finding out which Switch option is currently active

**URL:** <https://forums.powershell.org/t/finding-out-which-switch-option-is-currently-active/16036>\
**Category:** PowerShell Help\
**Created:** [March 21, 2021, 2:58am UTC](https://forums.powershell.org/t/finding-out-which-switch-option-is-currently-active/16036 "2021-03-21T02:58:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lem](https://avatars.discourse-cdn.com/v4/letter/l/74df32/32.png) [@Lem](https://forums.powershell.org/u/Lem)\
**Post date:** [March 21, 2021, 2:58am UTC](https://forums.powershell.org/t/finding-out-which-switch-option-is-currently-active/16036/1 "2021-03-21T02:58:35Z")

</div>

Hi, I’m another novice PowerShell user looking to get some help as I try to familiarize myself with the basics.

I’m using a version of the following code (that I found online) to be able to switch between different prompt styles.

What I’d like to know is, how can I create a function to “query” (don’t know if that’s the right term) which prompt style is currently being used and display its name on screen?

In other words, I’d type a command (for example, something like `get-prompt` or whatever) and it would then tell me the name of the currently active prompt style (as currently defined by `set-prompt`).

I can think of many instances where this type of thing would be useful, so finding out how to do it for this set-prompt example would give me a template for how it could be applied to other situations.

Unfortunately, despite lots of reading on various sites, I still don’t really know where to start with this.

Hopefully someone can help. Thanks.

```auto
Function Set-Prompt
{
    Param
    (
        [Parameter(Position=0)]
        [ValidateSet("Default","Basic","Other")]
        $Action
    )
    
    switch ($Action)
    {
        "Basic"
        {
            Function global:prompt
            {
                $Null
            }
        }
        
        "Other"
        {
            function global:prompt {

            #check and see if logon server is the same as the computername
            if ( $env:logonserver -ne "\\$env:computername" ) {
            #strip off the \\
            $label = ($env:logonserver).Substring(2)
            $color = "Green"
            }
            else {
            $label = "Not Connected"
            $color = "gray"
            }
             
            Write-Host ("[$label]") -ForegroundColor $color -NoNewline
            Write (" PS " + (Get-Location) + "&gt; ")
            }
        }
                
        default
        {
            Function global:prompt
            {
                  $(if (test-path variable:/PSDebugContext) { '[DBG]: ' } 
                  else { '' }) + 'PS ' + $(Get-Location) `
                  + $(if ($nestedpromptlevel -ge 1) { '&gt;&gt;' }) + '&gt; '
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 21, 2021, 3:52am UTC](https://forums.powershell.org/t/finding-out-which-switch-option-is-currently-active/16036/2 "2021-03-21T03:52:37Z")

</div>

Welcome to the forums.

Hmmm … actually I never felt the need for a function to change the prompt during a Powershell session. I’m used to set up my prompt in my profile to look like I want it to.

But because of prompt is actually just a built-in function you can overwrite if you wish you could set a variable representing the current state while you change the prompt function. Something similar to the preference variables like $DebuPreference or $VerbosePreference … maybe $PromptPreference or $PromptStyle. 😉

If you want to show the actual function code you could use

```auto
$Function:prompt

```

---

<div class="post-metadata">

**Author:** ![Lem](https://avatars.discourse-cdn.com/v4/letter/l/74df32/32.png) [@Lem](https://forums.powershell.org/u/Lem)\
**Post date:** [March 21, 2021, 6:05am UTC](https://forums.powershell.org/t/finding-out-which-switch-option-is-currently-active/16036/3 "2021-03-21T06:05:47Z")

</div>

> … you could set a variable representing the current state while you change the prompt function

How would I do that?

I don’t need to the the actual function code that is shown by `$Function:prompt` I only require the name of the chosen style (I think it might be called the switch ‘Parameter’). So in the case of the code example shown in my first post, it would display either `Default` `Basic` or `Other` (because those the available options listed in `ValidateSet`)

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 21, 2021, 7:24am UTC](https://forums.powershell.org/t/finding-out-which-switch-option-is-currently-active/16036/4 "2021-03-21T07:24:41Z")

</div>

> [@Lem](#):
>
> How would I do that?

You declare a ( maybe global ) variable inside the script block where you define the prompt function.

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [March 21, 2021, 7:26am UTC](https://forums.powershell.org/t/finding-out-which-switch-option-is-currently-active/16036/5 "2021-03-21T07:26:47Z")

</div>

Since the variable is used only to decide in the switch statement, it will not be exposed.  
I think naming the variable specific and making it global will help as the global variable will be accessible across the session till it ends.  
at the end of the function you can set a global variable like below

```auto
$Global:PromptSyle=$Action

```

Then this variable should be present across the session.

---

<div class="post-metadata">

**Author:** ![Lem](https://avatars.discourse-cdn.com/v4/letter/l/74df32/32.png) [@Lem](https://forums.powershell.org/u/Lem)\
**Post date:** [March 22, 2021, 3:05am UTC](https://forums.powershell.org/t/finding-out-which-switch-option-is-currently-active/16036/6 "2021-03-22T03:05:52Z")

</div>

Thank you, [kvprasoon](https://forums.powershell.org/t/finding-out-which-switch-option-is-currently-active/16036/5)

> at the end of the function …

I apologize for my cluelessness, but I don’t know where exactly to put it. At the end of _which_ function?

The obvious place seemed at the end of `Set'Prompt`, so I placed it before the last closing brace of the function, but when I then type `$PromptSyle` nothing is returned.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 22, 2021, 7:41am UTC](https://forums.powershell.org/t/finding-out-which-switch-option-is-currently-active/16036/7 "2021-03-22T07:41:31Z")

</div>

It works actually for me. Just your “_Basic_” prompt does not work on my test system. I think you cannot use $Null as the function code for prompt.

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [March 22, 2021, 11:08am UTC](https://forums.powershell.org/t/finding-out-which-switch-option-is-currently-active/16036/8 "2021-03-22T11:08:14Z")

</div>

It will be good if we can see your code. Do remove your secrets with samples when you share.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:27pm UTC](https://forums.powershell.org/t/finding-out-which-switch-option-is-currently-active/16036/9 "2024-05-16T20:27:02Z")

</div>

Closed on May 16, 2024, 8:27pm UTC


