2

You can create coloured text in the command prompt, but when it runs it's really slow. Is there any way of making it faster?

My Code:

@echo off
cls
powershell write-host -fore Yellow -back Red "Merry" -NoNewLine
powershell write-host -fore Red -back Yellow "Christmas" -NoNewLine
powershell write-host -fore Yellow -back Red "To" -NoNewLine
powershell write-host -fore Red -back Yellow "Everyone" -NoNewLine
powershell write-host -fore Yellow -back Red "In" -NoNewLine
powershell write-host -fore Red -back Yellow "2020!" -NoNewLine
1
  • 2
    If speed is your main concern and you're using Windows 10, I highly recommend using VT100 escape sequences instead of Powershell. Commented Dec 25, 2020 at 19:03

1 Answer 1

6

Powershell is an external program, that has to be loaded each time it is used. So use it just once:

@echo off
cls
powershell write-host -fore Yellow -back Red "Merry" -NoNewLine; ^
write-host -fore Red -back Yellow "Christmas" -NoNewLine;^
write-host -fore Yellow -back Red "To" -NoNewLine;^
write-host -fore Red -back Yellow "Everyone" -NoNewLine;^
write-host -fore Yellow -back Red "In" -NoNewLine;^
write-host -fore Red -back Yellow "2020!" -NoNewLine

Powershell has to be loaded just once compared to 6 times before.

The ^ at the end of the lines is a "Line continuation", so actually, this is just one long (logical) line, splitted into several (physical) lines for readabilty:

powershell write-host -fore Yellow -back Red "Merry" -NoNewLine;write-host -fore Red -back Yellow "Christmas" -NoNewLine;write-host -fore Yellow -back Red "To" -NoNewLine;write-host -fore Red -back Yellow "Everyone" -NoNewLine;write-host -fore Yellow -back Red "In" -NoNewLine;write-host -fore Red -back Yellow "2020!" -NoNewLine

Note, cmd has a maximal line length (about ~8k chars), which limits this method.

Sign up to request clarification or add additional context in comments.

4 Comments

I put it in a batch file and it says: ^ : The term '^' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
doesn't happen here. But I can replicate your error by ending the lines with ^^ instead of just ^. Please verify, there is just one ^ and it is the very last char on that (physical) line(s).
Question: do you start the batch file from a CMD window or from a Powershell Window?
the described error happens in a Powershell window; but it makes no sense to start powershell within powershell. Just use write-host in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.