A command prompt is used to prompts the user to action, current status, such as the current directory, the username and device name or others variables/function user-defined.
Define a function called Prompt and put it into the PowerShell profile, to view a profile path, display the value of the $PROFILE
variable.
Sample
A unix-like prompt here:
Function Prompt {
$CurrentDir = Split-Path $PWD -Leaf
if ($PWD -like (Get-PsProvider 'FileSystem').Home) {
$CurrentDir = '~'
}
# Python Pipenv
# if ($env:PIPENV_ACTIVE -eq 1) {
# $venv = (($env:VIRTUAL_ENV -split "\\")[-1] -split "-")[0]
# Write-Host ("($venv) ") -NoNewLine -ForegroundColor Red
# }
if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
$Symbol = '#'
} else {
$Symbol = '$'
}
Write-Host ("`[$env:USERNAME@$env:COMPUTERNAME $CurrentDir`]$Symbol") -NoNewLine -ForegroundColor DarkGreen
return " "
}
Preview
[user@device ~]$ (pwd).Path
C:\Users\user
[user@device ~]$ cd d:
[user@device D:\]$ mkdir foo | Out-Null; cd foo
[user@device foo]$ (pwd).Path
D:\foo
[user@device foo]$ sudo pwsh --nologo
[user@device foo]# Stop-Service ssh-agent; exit
[user@device foo]$