0

New to shell scripting, so trying to learn, I have written the following script:

Write-Output "Would you like to create a text file?"
$userInput = Read-Host "Y [1] Or N [2]? "

if ($userInput = 1)
{
Write-Output "Create file"
}
elseif ($userInput = 2)
{
Write-Output "No file created."
}
else
{
Write-Output "Incorrect entry."
}

Selecting Option 1 prints out Create file as expected.

Select Option 2 also prints out Create file, which is not expected.

Is is due to a quirk with how Read-Host works with saving variables, or am I using the wrong module for this task?

Thanks very much!

saturnSam
  • 21
  • 4

1 Answers1

1

Change with -eq operator (not use "="), below the code:

Write-Output "Would you like to create a text file?"
$userInput = Read-Host "Y [1] Or N [2]? "

if ($userInput -eq 1)
{
Write-Output "Create file"
}
elseif ($userInput -eq 2)
{
Write-Output "No file created."
}
else
{
Write-Output "Incorrect entry."
}
NickMartin
  • 91
  • 5