1

I am running Windows 10 and want to set up cmd.exe prompt such that it looks the same as my bash prompt on the same machine. I have set up a startup script to set the PROMPT variable when loading cmd.exe, which detects whether the shell is running with root privileges and set up the colour accordingly:

Shell colours

However, as shown from the screenshot, I can't set up the prompt in cmd.exe (the upper windows) such that the colour resets to default after finishing the command, as shown in bash (the lower windows). If I run bash from cmd, the colour also can't reset.

Then I run ls with colours in both shells, if I run it in cmd, or bash within cmd, the colour cannot be reset by the normal file. However, if I run it in bash only, the colour can be reset.

ls colours

How can I set up my startup script such that the colour behaviour of cmd.exe is made identical to my bash shell?

3
  • out of interest, how are you getting the bash prompt there in windows?
    – barlop
    Mar 21 '17 at 17:03
  • I installed msys2 Mar 22 '17 at 6:11
  • Another option to @MikeWise's answer below would be to use ConEmu as a terminal manager, which I personally would recommend anyways since it allows complete customization of the terminal. Microsoft's new Terminal is also highly customizable from it's .json config file, but ConEmu is still easier to customize and run multiple shells from.
    – JW0914
    Apr 19 '20 at 12:00
0

There are some newish experimental features in the Windows 10 console that make it much more *NIX like. For example colors that are no longer constrained to the 16 that were available up until now, cursor shapes, etc.

It is configured from the properties windows as before:

enter image description here

More details can be found here:

https://devblogs.microsoft.com/commandline/new-experimental-console-features/

0

One option would be to use ConEmu (customized conemu.xml) as a terminal manager since it allows complete customization of the terminal, combined with Powershell's profile.ps1

  • cmd specific commands can be run in Powershell by prefacing the command with cmd /c

%UserProfile%\Documents\WindowsPowerShell\profile.ps1

#

            ##::[[--- PowerShell PS1 Profile ---]]::##

#================================================================

  # Parameters
#----------------------------------------------------------------

# ANSI:
  $ESC  = [char]27

# Host.PrivateData
  $PD   = $($Host.PrivateData)


  # Colors
#----------------------------------------------------------------

# Powershell
  $Host.UI.RawUI.BackgroundColor  = ($bckgrnd = 'Black')
  $Host.UI.RawUI.ForegroundColor  = 'Gray'

  $PD.ErrorForegroundColor        = 'Red'
  $PD.ErrorBackgroundColor        = $bckgrnd

  $PD.WarningForegroundColor      = 'Magenta'
  $PD.WarningBackgroundColor      = $bckgrnd

  $PD.DebugForegroundColor        = 'Yellow'
  $PD.DebugBackgroundColor        = $bckgrnd

  $PD.VerboseForegroundColor      = 'Green'
  $PD.VerboseBackgroundColor      = $bckgrnd

  $PD.ProgressForegroundColor     = 'Yellow'
  $PD.ProgressBackgroundColor     = $bckgrnd

  #Clear-Host


  # Prompt
#----------------------------------------------------------------

Function set-prompt {
  Param (
    [Parameter(Position=0)]
    [ValidateSet("Default","Test")]
    $Action
  )

  switch ($Action) {

    "Default" {
      Function global:prompt {
        if (test-path variable:/PSDebugContext) { '[DBG]: ' }
          write-host " "
          write-host ("$ESC[48;2;40;40;40m$ESC[38;2;170;210;0m$(Get-Location) $ESC[0m $ESC[0m")

        if ( $host.UI.RawUI.WindowTitle -match "Administrator" ) {
          $Host.UI.RawUI.ForegroundColor = 'Red'
          $(if ($nestedpromptlevel -ge 1) {
            write-host ('PS $$ ') -ForegroundColor Red -NoNewLine
          } else {
            write-host ('PS $ ') -ForegroundColor Red -NoNewLine
          })
        } else {
          $(if ($nestedpromptlevel -ge 1) {
            write-host ('PS $$ ') -ForegroundColor Blue -NoNewLine
          } else {
            write-host ('PS $ ') -ForegroundColor Blue -NoNewLine
          })
        }

        return " "
      }
    }


    "Test" {
      function prompt() {
        if ( Test-Wow64 ) {
          write-host -NoNewLine "Wow64 "
        }
        if ( Test-Admin ) {
          write-host -NoNewLine -f red "Admin "
        }
        write-host -NoNewLine -ForegroundColor Green $(get-location)
        foreach ( $entry in (get-location -stack)) {
          write-host -NoNewLine -ForegroundColor Red '+';
        }
        write-host -NoNewLine -ForegroundColor Green '>'
        ' '
      }
    }
  }
}

set-prompt Default
  • Shows a colorized-text prompt for the user and a red-text prompt for an Admin
  • Allows on-the-fly switching of profiles (reload/switch prompt via set-prompt <name>)
    • Copy/paste Default section below itself and edit accordingly (see Test)


Microsoft's new Terminal is also highly customizable from it's .json config file, but ConEmu is easier to customize and run multiple shells from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.