0

When I add the code below to C:\Users\USERFOLDER\Documents\PowerShell\Microsoft.PowerShell_profile.ps1:

function prompt  
{  
    $ESC = [char]27
    "$ESC[46mPS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) $ESC[0m"  
}

(as suggested in How to colorize the Powershell prompt?)

My prompt then omits conda's environment prefix (e.g. base), despite the environment being activated.

How can I have a colored prompt but still keep conda's prefix?

Other related questions that do not answer mine:

Answers here that did not solve the problem either:

function prompt {
    $envName = (Get-Command conda | Select-String "activate" -Context 0,1).Context.PostContext -replace '>',''
    $promptColor = "Yellow"  # Change color as desired
Write-Host "[$envName] " -NoNewline -ForegroundColor $promptColor
return "PS 

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

And the following seemed to be the other suggestion, but it wasn't clear to me from the code posted:

function prompt {
    write-host "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " -foregroundcolor red -nonewline
}

I also tried

function prompt{
    write-host "PS " -ForegroundColor Magenta -NoNewline
    return "$((Get-Location).path)> "
}

But the conda environment prefix is still not displayed: enter image description here

4 Answers4

2

To use colors in powershell, you do not actually need any escape codes.

You can just do a write-host "text" -foregroundcolor red -nonewline to print a red color. The next write-host will be placed directly after due to the -nonewline switch.

Here's what my prompt looks like:

function Prompt
{
write-host " PS " -BackgroundColor darkred -ForegroundColor red -NoNewline
write-host "░▒▓█" -BackgroundColor darkred -ForegroundColor blue -NoNewline
write-host (get-date -Format "yyyy-MM-dd HH:mm:ss") -backgroundcolor blue -ForegroundColor white -NoNewline
write-host " ░▒▓█" -BackgroundColor Blue -ForegroundColor darkYellow  -NoNewline
write-host "░▒▓█" -BackgroundColor darkyellow -ForegroundColor Yellow  -NoNewline
write-host "\\$env:COMPUTERNAME " -BackgroundColor yellow -foregroundcolor black -NoNewline
write-host " ░▒▓█" -BackgroundColor Yellow -ForegroundColor Cyan  -NoNewline
if( (Get-Location).Drive -ne $null)
{
    write-host (Get-Location) -BackgroundColor Cyan -ForegroundColor Black -NoNewline
    $networkdrive = $false
    write-host "█▓▒" -ForegroundColor Cyan -BackgroundColor DarkCyan -NoNewline
    write-host "█▓▒" -ForegroundColor DarkCyan 
}
else
{
    $networkdrive = $true
    $first, $second, $third, $folder = (Get-Location).path.Split("\")
    write-host "\" -NoNewline -BackgroundColor Cyan -ForegroundColor Black
    $folder | foreach-object {
        write-host "\$_" -NoNewline -BackgroundColor Cyan -ForegroundColor Black
    }
    write-host "█▓▒" -ForegroundColor Cyan -BackgroundColor DarkCyan -NoNewline
    write-host "█▓▒" -ForegroundColor DarkCyan 
}
write-host "PS " -ForegroundColor Magenta -NoNewline
if( (Get-Location).Drive -ne $null)
{
    write-host "$((Get-Location).Drive):\"  -NoNewline
}
else
{
    write-host "\\$((Get-Location).path.Split("\")[3])\"  -NoNewline
}

if( (Get-Location).path.Split("\").Count -gt 2 -and $networkdrive -eq $false)
{
    write-host "…\"  -NoNewline
}
if( (Get-Location).path.Split("\").Count -gt 5 -and $networkdrive -eq $true)
{
    write-host "…\"  -NoNewline
}    write-host "$((Get-Location).path.Split("\")[-1])"  -NoNewline
write-host ">" -NoNewline
return " "

}

enter image description here

LPChip
  • 66,193
1

To have a colored PowerShell prompt and still display the Conda environment label, you can customize your PowerShell profile. You'll need to modify the prompt function to include both the color formatting and the Conda environment label. Here's a short guide:

Open your PowerShell profile script (Microsoft.PowerShell_profile.ps1 for PowerShell 7, or profile.ps1 for Windows PowerShell) in a text editor.

Add or modify the prompt function to include both color formatting and the Conda environment label. Here's an example:

function prompt {
    $envName = (Get-Command conda | Select-String "activate" -Context 0,1).Context.PostContext -replace '>',''
    $promptColor = "Yellow"  # Change color as desired
Write-Host "[$envName] " -NoNewline -ForegroundColor $promptColor
return "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "

}

Save the changes to your PowerShell profile script.

Restart your PowerShell session for the changes to take effect.

This modified prompt function will display the Conda environment label in square brackets with the specified color, followed by the standard PowerShell prompt. Adjust the color and formatting to suit your preferences.

Toto
  • 19,304
1

I Checked the Prompt function set by conda with (Get-Command Prompt). Make sure result has Conda at the Source Column like below.

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        prompt                                             0.0        Conda

Then check the ScriptBlock of the function with (Get-Command Prompt).ScriptBlock. It will look like this.


        if ($Env:CONDA_PROMPT_MODIFIER) {
            $Env:CONDA_PROMPT_MODIFIER | Write-Host -NoNewline
        }
        CondaPromptBackup;

And Modified just a bit and applied in the profile.ps1.

function Prompt {
    if ($Env:CONDA_PROMPT_MODIFIER) {
        "$Env:CONDA_PROMPT_MODIFIER$(pwd) " | Write-Host -NoNewline -ForegroundColor "Green"
    }
    CondaPromptBackup;
}

After that, prompt will look like this.

I don't have enough reputation to link img. Clink the link to see the result.

prompt result.

I tried to move the PS> but didn't worked out. At least the prompt got more readable.

SugOO
  • 26
0

Add the code below to C:\Users\USERFOLDER\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

function prompt {
       $envMatch = conda env list | Select-String -Pattern '[a-z ]* \*'  # find activated environment, it has an asterisk after its name
       if($envMatch -ne $null){
           $envName = "(" + ($envMatch.Matches[0].Value | Select-String -Pattern '[a-z]*').Matches[0].Value + ") "
       }
       else{  # if no environment is activated
           $envName = ""
       }
       $promptColor = "Green"  # Put whatever color you prefer
   Write-Host ("$envName" + "PS ") -NoNewline -ForegroundColor $promptColor
   return "$($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "

}

This introduces a slight (probably less than 0.5s) to the prompt which I couldn't get rid of.

From my tests, it seems the delay is cause by the line with (...) conda env list | Select-String -Pattern '[a-z ]* \*'(...). I find the delay a little annoying.