How to echo with different colors in the Windows command line

前端 未结 23 1631
野性不改
野性不改 2020-11-22 08:55

I know that the color bf command sets the colors of the whole command line window but I wanted to to print one single line in a different color.

23条回答
  •  无人共我
    2020-11-22 09:27

    This isn't a great answer, but if you know the target workstation has Powershell you can do something like this (assuming BAT / CMD script):

    CALL:ECHORED "Print me in red!"
    
    :ECHORED
    %Windir%\System32\WindowsPowerShell\v1.0\Powershell.exe write-host -foregroundcolor Red %1
    goto:eof
    

    Edit: (now simpler!)

    It's an old answer but I figured I'd clarify & simplify a bit

    img

    PowerShell is now included in all versions of Windows since 7. Therefore the syntax for this answer can be shortened to a simpler form:

    • the path doesn't need to be specified since it should be in the environment variable already.
    • unambiguous commands can be abbreviated. For example you can:
      • use -fore instead of -foregroundcolor
      • use -back instead of -backgroundcolor
    • the command can also basically be used 'inline' in place of echo
      (rather than creating a separate batch file as above).

    Example:

    powershell write-host -fore Cyan This is Cyan text
    powershell write-host -back Red This is Red background
    

    More Information:

    The complete list of colors and more information is available in the
    - PowerShell Documentation for Write-Host

提交回复
热议问题