본문 바로가기
ICT

[PowerShell]5. Controls(If statement)

by NeoSailer 2023. 3. 28.

When executing a script, there are cases to control computation or process accoding to result of pre-process or conditions. PowerShell provides "if" and "switch" state in the similar grammar of C language.

 

["if" Statement]

If statement has two parts, evaluating condition and executing code.

if (condition)
{
    # code to execute if condition is true
}

Below is an example of if statement

PS C:\PowerShell> Write-Host "Let's have a party!"
$sex = Read-Host "What is your sex? Male for 'M' female for 'F'"

if($sex -eq "M")
{
    Write-Host "Sorry the party is full, later!"
}
Let's have a party!
What is your sex? Male for 'M' female for 'F': M
Sorry the party is full, later!

* Evaluating conditions in "If" statement

  • -eq : equal to
  • -ne : not equal to
  • -lt : less than
  • -le : less than or equal to
  • -gt : greater than
  • -ge : greater than or equal to
  • -like : matches a wildcard pattern
  • -notlike : does not match a wildcard pattern
  • -match : matches a regular expression
  • -notmatch : does not match a regular expression

In case when executing different code according to condition, "else" statement can be added

PS C:\PowerShell> Write-Host "Let's have a party!"
$sex = Read-Host "What is your sex? Male for 'M' female for 'F'"

if($sex -eq "M")
{
    Write-Host "Sorry the party is full, later!"
}
else
{
    Write-Host "Please come in!"
}
Let's have a party!
What is your sex? Male for 'M' female for 'F': F
Please come in!

When conditions have more than two options, "elseif"(else if) can be used and for all other cases which do not match the given condition will go to "else" statement code.

PS C:\PowerShell> Write-Host "Let's have a party!"
$sex = Read-Host "What is your sex? Male for 'M' female for 'F'"

if($sex -eq "M")
{
    Write-Host "Sorry the party is full, later!"
}
elseif($sex -eq "F")
{
    Write-Host "Please come in!"
}
else
{
    Write-Host "What are you? No room for you"
}
Let's have a party!
What is your sex? Male for 'M' female for 'F': G
What are you? No room for you

["switch" Statement]

"switch" statement is similar to the "if" statement but it can be used with large number of conditions.

switch (expression)
{
    default { # code to execute if expression doesn't match any other values }
    value1 { # code to execute if expression equals value1 }
    value2 { # code to execute if expression equals value2 }    
}

Below is an example of "switch" statement

PS C:\PowerShell> $day = "Monday"

switch ($day)
{
    default { Write-Host "It's the weekend!" }
    "Monday" { Write-Host "It's Monday!" }
    "Tuesday" { Write-Host "It's Tuesday!" }
    "Wednesday" { Write-Host "It's Wednesday!" }
    "Thursday" { Write-Host "It's Thursday!" }
    "Friday" { Write-Host "It's Friday!" }    
}
It's Monday!

In convention, "switch" statement is hardly used unless condition has large number of conditions.

PS C:\PowerShell> $numberOfconditons = 3
if($numberOfconditons -le 4)
{
    Write-Host "Please use 'if' statement"
}
else
{
    Write-Host "hmm... ok go with 'switch' statement"
}
Please use 'if' statement

 

반응형

댓글