Read-Host with multiple colors











up vote
0
down vote

favorite












In one of my powershell functions, I want to gather input from the user, but first I need to give some instructions. I'd like to print a line or two in the console with different colors.



function myFunction(){
param(
[string]$directions = $(read-host "Please answer the questions according to your opinion`nYour answers must be Star Wars-based." -foregroundcolor "Magenta"),
[string]$robot = $(read-host "What is your favourite robot" -foregroundcolor "Yellow"),
[string]$spaceship = $(read-host "What is your favourite spaceship" -foregroundcolor "Green")
)
write-host "Favourite Robot = " + $robot
write-host "Favourite Spaceship = " + $spaceship
}
#call the function
myFunction


In the function above I have a newline to keep the directions on separate levels, but I want the first line of this text to be one colour and the second line to be another colour.



Also, -foregroundcolor doesn't work here - it just prints literally.



I can't put a write-host before the param statement or I would put the directions there (I know how to do this with multiple colours).










share|improve this question


















  • 3




    Read-Host does not have a -ForegroundColor parameter.
    – Bill_Stewart
    Nov 19 at 22:06










  • You've answered your own question, you need to do it with Write-Host. Fun fact, it can be done despite your reservations.
    – Drew
    Nov 19 at 22:08










  • So you just need to use Write-Host "Some text" -ForegroundColor -nonewline; Read-Host
    – Owain Esau
    Nov 19 at 22:09










  • ex: Write-Host "This Is " -ForegroundColor RED -NoNewline; Write-Host "asking for coloured input: " -ForegroundColor YELLOW -NoNewline; Read-Host;
    – Owain Esau
    Nov 19 at 22:10










  • I know it can be done with write-host but how do I display the write-host before the parameters are retrieved?
    – bgmCoder
    Nov 19 at 22:15















up vote
0
down vote

favorite












In one of my powershell functions, I want to gather input from the user, but first I need to give some instructions. I'd like to print a line or two in the console with different colors.



function myFunction(){
param(
[string]$directions = $(read-host "Please answer the questions according to your opinion`nYour answers must be Star Wars-based." -foregroundcolor "Magenta"),
[string]$robot = $(read-host "What is your favourite robot" -foregroundcolor "Yellow"),
[string]$spaceship = $(read-host "What is your favourite spaceship" -foregroundcolor "Green")
)
write-host "Favourite Robot = " + $robot
write-host "Favourite Spaceship = " + $spaceship
}
#call the function
myFunction


In the function above I have a newline to keep the directions on separate levels, but I want the first line of this text to be one colour and the second line to be another colour.



Also, -foregroundcolor doesn't work here - it just prints literally.



I can't put a write-host before the param statement or I would put the directions there (I know how to do this with multiple colours).










share|improve this question


















  • 3




    Read-Host does not have a -ForegroundColor parameter.
    – Bill_Stewart
    Nov 19 at 22:06










  • You've answered your own question, you need to do it with Write-Host. Fun fact, it can be done despite your reservations.
    – Drew
    Nov 19 at 22:08










  • So you just need to use Write-Host "Some text" -ForegroundColor -nonewline; Read-Host
    – Owain Esau
    Nov 19 at 22:09










  • ex: Write-Host "This Is " -ForegroundColor RED -NoNewline; Write-Host "asking for coloured input: " -ForegroundColor YELLOW -NoNewline; Read-Host;
    – Owain Esau
    Nov 19 at 22:10










  • I know it can be done with write-host but how do I display the write-host before the parameters are retrieved?
    – bgmCoder
    Nov 19 at 22:15













up vote
0
down vote

favorite









up vote
0
down vote

favorite











In one of my powershell functions, I want to gather input from the user, but first I need to give some instructions. I'd like to print a line or two in the console with different colors.



function myFunction(){
param(
[string]$directions = $(read-host "Please answer the questions according to your opinion`nYour answers must be Star Wars-based." -foregroundcolor "Magenta"),
[string]$robot = $(read-host "What is your favourite robot" -foregroundcolor "Yellow"),
[string]$spaceship = $(read-host "What is your favourite spaceship" -foregroundcolor "Green")
)
write-host "Favourite Robot = " + $robot
write-host "Favourite Spaceship = " + $spaceship
}
#call the function
myFunction


In the function above I have a newline to keep the directions on separate levels, but I want the first line of this text to be one colour and the second line to be another colour.



Also, -foregroundcolor doesn't work here - it just prints literally.



I can't put a write-host before the param statement or I would put the directions there (I know how to do this with multiple colours).










share|improve this question













In one of my powershell functions, I want to gather input from the user, but first I need to give some instructions. I'd like to print a line or two in the console with different colors.



function myFunction(){
param(
[string]$directions = $(read-host "Please answer the questions according to your opinion`nYour answers must be Star Wars-based." -foregroundcolor "Magenta"),
[string]$robot = $(read-host "What is your favourite robot" -foregroundcolor "Yellow"),
[string]$spaceship = $(read-host "What is your favourite spaceship" -foregroundcolor "Green")
)
write-host "Favourite Robot = " + $robot
write-host "Favourite Spaceship = " + $spaceship
}
#call the function
myFunction


In the function above I have a newline to keep the directions on separate levels, but I want the first line of this text to be one colour and the second line to be another colour.



Also, -foregroundcolor doesn't work here - it just prints literally.



I can't put a write-host before the param statement or I would put the directions there (I know how to do this with multiple colours).







powershell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 22:00









bgmCoder

4,39973578




4,39973578








  • 3




    Read-Host does not have a -ForegroundColor parameter.
    – Bill_Stewart
    Nov 19 at 22:06










  • You've answered your own question, you need to do it with Write-Host. Fun fact, it can be done despite your reservations.
    – Drew
    Nov 19 at 22:08










  • So you just need to use Write-Host "Some text" -ForegroundColor -nonewline; Read-Host
    – Owain Esau
    Nov 19 at 22:09










  • ex: Write-Host "This Is " -ForegroundColor RED -NoNewline; Write-Host "asking for coloured input: " -ForegroundColor YELLOW -NoNewline; Read-Host;
    – Owain Esau
    Nov 19 at 22:10










  • I know it can be done with write-host but how do I display the write-host before the parameters are retrieved?
    – bgmCoder
    Nov 19 at 22:15














  • 3




    Read-Host does not have a -ForegroundColor parameter.
    – Bill_Stewart
    Nov 19 at 22:06










  • You've answered your own question, you need to do it with Write-Host. Fun fact, it can be done despite your reservations.
    – Drew
    Nov 19 at 22:08










  • So you just need to use Write-Host "Some text" -ForegroundColor -nonewline; Read-Host
    – Owain Esau
    Nov 19 at 22:09










  • ex: Write-Host "This Is " -ForegroundColor RED -NoNewline; Write-Host "asking for coloured input: " -ForegroundColor YELLOW -NoNewline; Read-Host;
    – Owain Esau
    Nov 19 at 22:10










  • I know it can be done with write-host but how do I display the write-host before the parameters are retrieved?
    – bgmCoder
    Nov 19 at 22:15








3




3




Read-Host does not have a -ForegroundColor parameter.
– Bill_Stewart
Nov 19 at 22:06




Read-Host does not have a -ForegroundColor parameter.
– Bill_Stewart
Nov 19 at 22:06












You've answered your own question, you need to do it with Write-Host. Fun fact, it can be done despite your reservations.
– Drew
Nov 19 at 22:08




You've answered your own question, you need to do it with Write-Host. Fun fact, it can be done despite your reservations.
– Drew
Nov 19 at 22:08












So you just need to use Write-Host "Some text" -ForegroundColor -nonewline; Read-Host
– Owain Esau
Nov 19 at 22:09




So you just need to use Write-Host "Some text" -ForegroundColor -nonewline; Read-Host
– Owain Esau
Nov 19 at 22:09












ex: Write-Host "This Is " -ForegroundColor RED -NoNewline; Write-Host "asking for coloured input: " -ForegroundColor YELLOW -NoNewline; Read-Host;
– Owain Esau
Nov 19 at 22:10




ex: Write-Host "This Is " -ForegroundColor RED -NoNewline; Write-Host "asking for coloured input: " -ForegroundColor YELLOW -NoNewline; Read-Host;
– Owain Esau
Nov 19 at 22:10












I know it can be done with write-host but how do I display the write-host before the parameters are retrieved?
– bgmCoder
Nov 19 at 22:15




I know it can be done with write-host but how do I display the write-host before the parameters are retrieved?
– bgmCoder
Nov 19 at 22:15












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You seem to be struggling a little with the instructions in the comments so here... Unsure why you want a read host for instructions but that's cool.



function myFunction(){
param(
[string]$directions = $(Write-Host "Please answer the questions according to your opinion`nYour answers must be Star Wars-based.: " -ForegroundColor Magenta -NoNewline; Read-Host),
[string]$robot = $(Write-Host "What is your favourite robot: " -ForegroundColor Yellow -NoNewline; Read-Host),
[string]$spaceship = $(Write-Host "What is your favourite spaceship: " -ForegroundColor Green -NoNewline; Read-Host)
)
write-host "Favourite Robot = "$robot
write-host "Favourite Spaceship = "$spaceship
}





share|improve this answer





















  • Instructions are for me... In my real function, I want to tell myself how to build a csv file just in case I forget later. Thanks for taking the time.
    – bgmCoder
    Nov 19 at 22:25












  • No worries Brother, be sure to post any sections of code that you may have problems with for your CSV project. A tip is to state the desired outcome, what you have tried and the section of code that is not producing the desired outcome.
    – Drew
    Nov 19 at 22:31











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383257%2fread-host-with-multiple-colors%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You seem to be struggling a little with the instructions in the comments so here... Unsure why you want a read host for instructions but that's cool.



function myFunction(){
param(
[string]$directions = $(Write-Host "Please answer the questions according to your opinion`nYour answers must be Star Wars-based.: " -ForegroundColor Magenta -NoNewline; Read-Host),
[string]$robot = $(Write-Host "What is your favourite robot: " -ForegroundColor Yellow -NoNewline; Read-Host),
[string]$spaceship = $(Write-Host "What is your favourite spaceship: " -ForegroundColor Green -NoNewline; Read-Host)
)
write-host "Favourite Robot = "$robot
write-host "Favourite Spaceship = "$spaceship
}





share|improve this answer





















  • Instructions are for me... In my real function, I want to tell myself how to build a csv file just in case I forget later. Thanks for taking the time.
    – bgmCoder
    Nov 19 at 22:25












  • No worries Brother, be sure to post any sections of code that you may have problems with for your CSV project. A tip is to state the desired outcome, what you have tried and the section of code that is not producing the desired outcome.
    – Drew
    Nov 19 at 22:31















up vote
1
down vote



accepted










You seem to be struggling a little with the instructions in the comments so here... Unsure why you want a read host for instructions but that's cool.



function myFunction(){
param(
[string]$directions = $(Write-Host "Please answer the questions according to your opinion`nYour answers must be Star Wars-based.: " -ForegroundColor Magenta -NoNewline; Read-Host),
[string]$robot = $(Write-Host "What is your favourite robot: " -ForegroundColor Yellow -NoNewline; Read-Host),
[string]$spaceship = $(Write-Host "What is your favourite spaceship: " -ForegroundColor Green -NoNewline; Read-Host)
)
write-host "Favourite Robot = "$robot
write-host "Favourite Spaceship = "$spaceship
}





share|improve this answer





















  • Instructions are for me... In my real function, I want to tell myself how to build a csv file just in case I forget later. Thanks for taking the time.
    – bgmCoder
    Nov 19 at 22:25












  • No worries Brother, be sure to post any sections of code that you may have problems with for your CSV project. A tip is to state the desired outcome, what you have tried and the section of code that is not producing the desired outcome.
    – Drew
    Nov 19 at 22:31













up vote
1
down vote



accepted







up vote
1
down vote



accepted






You seem to be struggling a little with the instructions in the comments so here... Unsure why you want a read host for instructions but that's cool.



function myFunction(){
param(
[string]$directions = $(Write-Host "Please answer the questions according to your opinion`nYour answers must be Star Wars-based.: " -ForegroundColor Magenta -NoNewline; Read-Host),
[string]$robot = $(Write-Host "What is your favourite robot: " -ForegroundColor Yellow -NoNewline; Read-Host),
[string]$spaceship = $(Write-Host "What is your favourite spaceship: " -ForegroundColor Green -NoNewline; Read-Host)
)
write-host "Favourite Robot = "$robot
write-host "Favourite Spaceship = "$spaceship
}





share|improve this answer












You seem to be struggling a little with the instructions in the comments so here... Unsure why you want a read host for instructions but that's cool.



function myFunction(){
param(
[string]$directions = $(Write-Host "Please answer the questions according to your opinion`nYour answers must be Star Wars-based.: " -ForegroundColor Magenta -NoNewline; Read-Host),
[string]$robot = $(Write-Host "What is your favourite robot: " -ForegroundColor Yellow -NoNewline; Read-Host),
[string]$spaceship = $(Write-Host "What is your favourite spaceship: " -ForegroundColor Green -NoNewline; Read-Host)
)
write-host "Favourite Robot = "$robot
write-host "Favourite Spaceship = "$spaceship
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 22:20









Drew

1,148416




1,148416












  • Instructions are for me... In my real function, I want to tell myself how to build a csv file just in case I forget later. Thanks for taking the time.
    – bgmCoder
    Nov 19 at 22:25












  • No worries Brother, be sure to post any sections of code that you may have problems with for your CSV project. A tip is to state the desired outcome, what you have tried and the section of code that is not producing the desired outcome.
    – Drew
    Nov 19 at 22:31


















  • Instructions are for me... In my real function, I want to tell myself how to build a csv file just in case I forget later. Thanks for taking the time.
    – bgmCoder
    Nov 19 at 22:25












  • No worries Brother, be sure to post any sections of code that you may have problems with for your CSV project. A tip is to state the desired outcome, what you have tried and the section of code that is not producing the desired outcome.
    – Drew
    Nov 19 at 22:31
















Instructions are for me... In my real function, I want to tell myself how to build a csv file just in case I forget later. Thanks for taking the time.
– bgmCoder
Nov 19 at 22:25






Instructions are for me... In my real function, I want to tell myself how to build a csv file just in case I forget later. Thanks for taking the time.
– bgmCoder
Nov 19 at 22:25














No worries Brother, be sure to post any sections of code that you may have problems with for your CSV project. A tip is to state the desired outcome, what you have tried and the section of code that is not producing the desired outcome.
– Drew
Nov 19 at 22:31




No worries Brother, be sure to post any sections of code that you may have problems with for your CSV project. A tip is to state the desired outcome, what you have tried and the section of code that is not producing the desired outcome.
– Drew
Nov 19 at 22:31


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383257%2fread-host-with-multiple-colors%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen