Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Press X to exit or any other key to continue?

Tags:

powershell

Grrr... Noob question here. I want to create a block that basically does this:

Write-Host "Press X to cancel or any other key to continue"
$continue = Read-Host
  If ($continue = "X") 
     {exit}
  else 
     {Write-Host "Hello world"}

Keeps exiting even if I press another key... What am I doing wrong? Thanks!!!

like image 852
Dominic Brunetti Avatar asked Jan 15 '23 01:01

Dominic Brunetti


2 Answers

You should use "-eq" for comparsion. Simple example:

$a = "Powershell"
IF ($a -eq "PowerShell")
{
 "Statement is True"
}
ELSE
{
 "Statement is False"
}

Here is some reading to be confident with "if-then-else" statements: IF_THEN_ELSE in Powershell

like image 131
Mikhail Kalashnikov Avatar answered Jan 31 '23 05:01

Mikhail Kalashnikov


The = operator is for assignment. Use -eq to test for equality.

like image 26
jeconner Avatar answered Jan 31 '23 05:01

jeconner