Author Posts

March 31, 2016 at 10:31 am

I have a custom prompt in my profile.

function prompt {
  $p = Split-Path -leaf -path (Get-Location)
  "$p> "
}

But when I am debugging a script, I noticed the prompt in suspended mode doesn't have the telltale '>>' I normally see on an non-customized session's prompt.

PS C:\windows> function testtest () {
>>     $DebugPreference = 'inquire'
>>     Write-Debug "test"
>> }
>>
PS C:\windows> testtest -debug
DEBUG: test
Confirm
Continue with this operation?
[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"): s

PS C:\windows>> 

How would I change my custom prompt to account for this (and any other) case where the prompt is altered for the user?

April 6, 2016 at 9:53 am

I think the key is to look at the default prompt code (gc function:prompt):

"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel
+ 1)) "

It's the $nestedPromptLevel bit that you're missing. See how it's multiplying ">" times the number of prompts, plus one?

April 13, 2016 at 3:22 pm

Thanks!
I've used the following and it works as expected 🙂

function prompt {
  $p = Split-Path -leaf -path (Get-Location)
  "PS $p$('>' * ($nestedPromptLevel + 1)) "
}