Make this if statement into a switch statement in Powershell
$Month = Read-Host "What month were you born in? Input a number 1-12"
if($month -eq 1) {
echo "You were born in: January"
}elseif ($Month -eq 2) {
echo "You were born in: Febuary"
}elseif ($Month -eq 3) {
echo "You were born in: March"
}elseif ($Month -eq 4) {
echo "You were born in: April"
}elseif ($Month -eq 5) {
echo "You were born in: May"
}elseif ($Month -eq 6) {
echo "You were born in: June"
}elseif ($Month -eq 7) {
echo "You were born in: July"
}elseif ($Month -eq 8) {
echo "You were born in: August"
}elseif ($Month -eq 9) {
echo "You were born in: September"
}elseif ($Month -eq 10) {
echo "You were born in: October"
}elseif ($Month -eq 11) {
echo "You were born in: November"
}elseif ($Month -eq 12) {
echo "You were born in: December"
}esle {"There are only 12 months in the calendar....please choose
between 1-12"}
Hi ,
Please find the code -
$Month = Read-Host "What month were you born in? Input a number 1-12"
switch($Month)
{
1 {"You were born in: January"}
2 {"You were born in: Febuary"}
3 {"You were born in: March"}
4 {"You were born in: April"}
5 {"You were born in: May"}
6 {"You were born in: June"}
7 {"You were born in: July"}
8 {"You were born in: August"}
9 {"You were born in: September"}
10 {"You were born in: October"}
11 {"You were born in: November"}
12 {"You were born in: December"}
Default {
"There are only 12 months in the calendar....please choose between 1-12"
}
}
output -
Thank You.
Get Answers For Free
Most questions answered within 1 hours.