Hi All,
I am trying to learn powershell and doing lab’s I have an error in my code and just can not for the life of me figure it out. any in site would be awesome.
#Main Body
#This section contains the code that implements that main function of the script.#
$menu
$menu_choice = Read-Host -Prompt “Choose a menu option”
$sure = Read-Host -Prompt “Are you sure you want to exit? (Y)”
if ($sure -eq “Y”)
{
Write-Host “This I can do.”
Write-Host “Exiting…”
}
Else
{
Write-Host “Not Exiting :)”
}
ElseIf ( $menu_choice -eq “X” )
{
$sure = Read-Host -prompt “are you sure you want to exit? (Y)”
{
Write-Host “This I can do.”
Write-Host “Exiting…”
}
Else
{
Write-host “Not Exiting :)”
}
}
1 Spice up
rockn
(Rockn)
2
What is the error? Use the insert code button when posting code please.
Elseif : The term ‘Elseif’ 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.
At C:\Users\Administrator\Scripts\Get-Inventorya.ps1:39 char:1
-
Elseif ( $menu_choice -eq “X” )
-
-
CategoryInfo : ObjectNotFound: (Elseif:String) , CommandNotFoundException
-
FullyQualifiedErrorId : CommandNotFoundException
Neally
(Neally)
4
If you post code, please use the ‘Insert Code’ button. Please and thank you!

#Main Body
#This section contains the code that implements that main function of the script.#
$menu
$menu_choice = Read-Host -Prompt “Choose a menu option”
$sure = Read-Host -Prompt “Are you sure you want to exit? (Y)”
if ($sure -eq “Y”)
{
Write-Host “This I can do.”
Write-Host “Exiting…”
}
Else
{
Write-Host "Not Exiting :)"
}
ElseIf ( $menu_choice -eq “X” )
{
$sure = Read-Host -prompt "are you sure you want to exit? (Y)"
{
Write-Host “This I can do.”
Write-Host “Exiting…”
}
Else
{
Write-host "Not Exiting :)"
}
}
Neally
(Neally)
6
really, look into switch()
2 Spice ups
Wanted to say the same but Neally’s too fast.
Neally
(Neally)
8
This is how it would look like with a switch, it might look ocnfusing, but it is actually WAY more comprehensive than several if else elseif etc.
Write-Output "1 - Red"
Write-Output "2 - Yellow"
Write-Output "3 - Green"
$menu_choice = Read-Host "Choose a menu option [1|2|3]"
switch($menu_choice){
1 {
Write-Host "You chose option 1 - Good choice!" -ForegroundColor Red
# Do option 1 stuff
}
2 {
Write-Host "You chose option 2 - Good choice!" -ForegroundColor Yellow
# Do option 2 stuff
}
3 {
Write-Host "You chose option 3 - Meh it was an ok choice!" -ForegroundColor Green
# Do option 3 stuff
}
default {
Write-Warning "Unexpected input"
}
}#end switch $menu_choice
$sure = Read-Host "Are you sure you want to exit? (Y)"
switch($sure){
y {
Write-Output "This I can do."
Write-Output "Exiting…"
}
n {
Write-Output "Not Exiting :)"
}
default {
Write-Warning "Unexpected input"
}
}#end switch $sure
@raysawyer2245
2 Spice ups
Neally
(Neally)
9
For your original question:
It is IF → ELSEIF → ELSE
if(<# some condition#>){
# do something
}elseif(<# some other condition#>){
# do something else
}else{
# do that
}
Also, formatting is really important, not as much for syntax in PowerShell but legibility.
But yeah, the code editor here in SW butchers things at time ¯_(ツ)_/¯
1 Spice up