(24) The IF condition in Poweshell

Source: Internet
Author: User

In the previous chapters, we focused on the development tools, variables, operators, and special variables of PowerShell, starting with this section to introduce the syntax structure in PowerShell.


    • This section focuses on if condition judgments in PowerShell. In addition, readers are expected to use powershell_ise to perform input and operation of the command from this section, Powershell_ise provides interface command input, parameter hints, self-complement, debug and other features, which is very helpful for writing the correct script.


If can be used to choose to run different commands based on the results of one or more condition tests. The use of the If method in Java, C #, C, and C + + programming languages is exactly the same.

syntax for the IF statement :

if (< test condition 1>) < statement block 1>}[elseif (< test condition 2>) {< statement block 2>}][else {< statement block 3>}]

When you run the IF statement, the PowerShell calculation < test condition 1> Whether the result of the conditional expression is true or false.

    • If the < test condition 1> is true, then the < statement block 1> is run, and PowerShell exits the IF statement.

    • If the < test condition 1> is false, the PowerShell calculation < test condition 2> The condition specified by the condition expression.

    • If the < test condition 2> is true, then the < statement block 2> is run, and PowerShell exits the IF statement.

    • If the < test condition 1> and < test conditions 2> evaluates to FALSE, the < statement block 3> code block is run, and PowerShell exits the IF statement.


Note : In other languages, else if the else and if are judged to be separate, but in PowerShell, two words are joined together and require special attention.


The following example illustrates the use of an if statement.

    1. The IF statement contains only one branch of judgment.

$a =3if ($a-gt 2) {write-host "The value $a is greater than 2."}

The $a=3 is defined at the beginning, and then a is judged, obviously the condition is established, and the final print

"The value $a is greater than 2."

2. You can also add an else branch to it, such as

$a =3if ($a-gt 2) {write-host "The value $a is greater than 2."} else{Write-host "The value $a is less than or equal to 2, is not created or is not initialized."}

3. Add ElseIf to achieve multiple judgments of the condition.

if ($a-gt 2) {write-host "The value $a is greater than 2."} ElseIf ($a-eq 2) {write-host "The value $a is equal to 2."} else{Write-host "The value $a is less than 2 or were not created or initialized."}


    • Summarize

The If judgment is used to determine whether the condition is true and to perform the corresponding operation. For example, determine whether the file or folder is known to exist, if it exists, write to the file, if it does not exist, create the file. such as determining whether the call is known to execute correctly, and so on.

This article from "Flower Blossom Fall" blog, declined reprint!

(24) The IF condition in Poweshell

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.