In PowerShell, Prompt
is by default, a built-in function that returns a string. In a PowerShell session where the prompt hasn't been customized, the underlying code can be viewed by typing:
(Get-Command prompt).ScriptBLock
PS C:\>
>> "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel +
>> 1)) ";
>> # .Link
>> # https://go.microsoft.com/fwlink/?LinkID=225750
>> # .ExternalHelp System.Management.Automation.dll-help.xml
>>
>>
You can use this code as the basis for a custom function that will override the default.
In the above, the path is obtained by this code:
$($executionContext.SessionState.Path.CurrentLocation)
To pare it down to the current directory name, change it to:
$($executionContext.SessionState.Path.CurrentLocation.Path.Split('\')[-1])
So to override the default prompt, declare a function of the same mane:
function Prompt {
"PS $($executionContext.SessionState.Path.CurrentLocation.Path.Split('\')[-1])$('>' * ($nestedPromptLevel +1)) "
}
To make the custom prompt the default for new sessions, add the function definition to your PowerShell profile. The following code will add it to your existing profile, or create your profile if you don't have one.
If (!(Test-Path -Path $PROFILE )) {
New-Item -Type File -Path $PROFILE -Force | out-null }
@'
function Prompt {
"PS $($executionContext.SessionState.Path.CurrentLocation.Path.Split('\')[-1])$('>' * ($nestedPromptLevel +1)) "
}
'@ | Add-Content -Path $PROFILE -Force