Finding out which Switch option is currently active

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.

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) + "> ")
            }
        }
                
        default
        {
            Function global:prompt
            {
                  $(if (test-path variable:/PSDebugContext) { '[DBG]: ' } 
                  else { '' }) + 'PS ' + $(Get-Location) `
                  + $(if ($nestedpromptlevel -ge 1) { '>>' }) + '> '
            }
        }
    }
}

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. :wink:

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

$Function:prompt

… 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)

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

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

$Global:PromptSyle=$Action

Then this variable should be present across the session.

2 Likes

Thank you, kvprasoon

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.

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.

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