Windows PowerShell's If StatementIntroduction to Windows PowerShell's If StatementPowerShell's 'If' statement comes under the umbrella of flow control. Once you master the basic construction then you can increase the usefulness by adding, 'Else' and 'ElseIf' statements. Topics for PowerShell PowerShell's If Statement
Construction of the 'If' StatementAs with so many PowerShell constructions, the type of bracket signifies how to break the script into sections. It is worth emphasising that (parenthesis are for the first part, the condition), while {braces are for the block command}. If (condition) {Do stuff} Example 1 Plain 'If'$Number = 10 Learning PointsNote 1: Trace the construction and separate into: if (test) and {what to do}. Note 2: Avoid over-think; there is no 'Then' in PowerShell's 'If' statement. Instead of worrying about 'Then', pay close attention to the two types of bracket. Note 3: To double check your understanding, try amending, "Bigger than Zero" to a different text string, such as: "Less than nought". Once you have done that, set $Number to -1. Example 1a File Content Example of Plain 'If'PowerShell has a batch of help files. One of these files contains help about the 'if' statement. In the example below, $file references that file. $Content is set to the content of the file. The third line attempts to match a string to the contents of the file. # Help on PowerShell's if statements Learning PointsThis example is concerned with matching a string "The if Statement" to the contents of a file. Example 2 'If' with 'Else'
# PowerShell's 'If' and 'Else' example Learning PointsThe best way to see how 'else' operates is to amend line 3 thus:
˚
Example 3 ElseIfThis example has a real task, and that is to check that we have the name of an actual file. # Introducing PowerShell's ElseIf Learning PointsThe advantage of ElseIf over plain Else, is that we can introduce a new test. In the above example we use ElseIf to check if the length of the file is less than 1. To activate the 'ElseIf' block, set
$file to a non-existent file for example If you have time, you could add more 'ElseIf' statements to cater for other eventualities. Summary of PowerShell's If ConstructionWhen it comes to filtering output, one of the oldest and best statements is the 'If' clause. As usual, the secret of understanding the syntax is to pay close attention to the style bracket. If (parenthesis for the test) and {braces for the action}. Once you have mastered the basic 'If' statement, then extend your capabilities by researching 'Else' and 'ElseIf'. Incidentally, the 'Vehicle' for our tests reveals a whole family of about_zyx files. My point is there is no command : 'get-help if'. However, if you look in the PowerShell directory then you will see 'About' files to assist with commands such as 'If' and 'ElseIf'. See Also PowerShell Tutorials• PowerShell Home • If Statement • Conditional Operators • Switch • Loops • Brackets Please write in if you see errors of any kind. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.
*
|
||||||