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).
powershell
|
show 2 more comments
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).
powershell
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 withWrite-Host
. Fun fact, it can be done despite your reservations.
– Drew
Nov 19 at 22:08
So you just need to useWrite-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 withwrite-host
but how do I display thewrite-host
before the parameters are retrieved?
– bgmCoder
Nov 19 at 22:15
|
show 2 more comments
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).
powershell
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
powershell
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 withWrite-Host
. Fun fact, it can be done despite your reservations.
– Drew
Nov 19 at 22:08
So you just need to useWrite-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 withwrite-host
but how do I display thewrite-host
before the parameters are retrieved?
– bgmCoder
Nov 19 at 22:15
|
show 2 more comments
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 withWrite-Host
. Fun fact, it can be done despite your reservations.
– Drew
Nov 19 at 22:08
So you just need to useWrite-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 withwrite-host
but how do I display thewrite-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
|
show 2 more comments
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
}
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
add a comment |
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
}
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
add a comment |
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
}
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
add a comment |
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
}
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
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 thewrite-host
before the parameters are retrieved?– bgmCoder
Nov 19 at 22:15