PowerShell Flow of Control Copyright © 2016 – Curt Hill
Introduction We have seen some of the basics of scripting now onto flow of control Everyone in this class knows C++ and one or more additional programming languages Covering flow of control should be comparatively easy The syntax may be different than what you have seen previously but the concepts should be easy to understand Copyright © 2016 – Curt Hill
Blocks PowerShell uses the braces { } in a similar way as the C family of languages They create a block of statements that is considered as a unit for inclusion in a decision or looping statement In the console entering a { keeps the item from being executed until the terminal brace is entered The scope rules are different from C++ and more similar to C Will be discussed later Copyright © 2016 – Curt Hill
Conditions Flow of control statements typically need some type of expression that generates a true/false value to drive them This is a problem for the traditional comparisons < and > are special characters for PowerShell Therefore PowerShell uses a FORTRANish set of operators Copyright © 2016 – Curt Hill
Comparison Operators Use a dash followed by two letters to indicate the operator == is –eq < is –lt <= is –le != is –ne > is –gt >= is –ge Copyright © 2016 – Curt Hill
Logical Operators We also have Boolean operators: Not is –not And is –and Or is –or Exclusive or is –xor Copyright © 2016 – Curt Hill
Other Operators There are others that are neither comparison nor Boolean operators Yet still handy -like and –notlike Uses wildcards -match and –notmatch Uses regular expressions -in and –notin -contains and –notcontains -is and –isnot Copyright © 2016 – Curt Hill
Case Sensitivity These operators are not case sensitive As are most MicroSoft comparisons If case sensitivity is desired prefix the operator with a c Thus: $item –eq “stuff” is case insensitive $item –ceq “Stuff” is case sensitive Copyright © 2016 – Curt Hill
Example Copyright © 2016 – Curt Hill
Decisions The language contains several flow statements If Switch For While Do foreach Copyright © 2016 – Curt Hill
If Other than the condition the simple forms look like C if($t –eq $other) { … } else { … } Do I need to say more? Copyright © 2016 – Curt Hill
Yes, I do PowerShell requires the braces after the then or else Makes parsing easier PowerShell also has an elseif Often used to reduce braces for nested if then elses Copyright © 2016 – Curt Hill
Examples Copyright © 2016 – Curt Hill
Switch Allows certain types of nested if then elses to be collapsed Different than that of C in that it is more clearly a modified if No case keyword instead use a conditional expression that references $_ which is the path selector Consider the following example Copyright © 2016 – Curt Hill
Switch example Clear enough? Copyright © 2016 – Curt Hill
For loops Very similar to that of C with the exception of variable names and conditionals See the next slide Copyright © 2016 – Curt Hill
For example Copyright © 2016 – Curt Hill
Whiles Similarly the while and do while look like those of C Copyright © 2016 – Curt Hill
While Example Copyright © 2016 – Curt Hill
Do While Example Copyright © 2016 – Curt Hill
More There is also a Pascalish Do Until Same as do while except condition is reversed Do while quits when condition is false Do until quits when condition is true Copyright © 2016 – Curt Hill
Foreach The foreach is an iterator through a collection The most interesting collections are returned by other objects Such as files in a directory You may create a collection with a comma separated list or a subrange Then the in operator may be used Copyright © 2016 – Curt Hill
Collections A subrange is two numeric values separated by a two dot ellipsis, such as: 4..9 If these are non-integer they will be truncated to an integer A comma separated list may be of any type Copyright © 2016 – Curt Hill
Example Copyright © 2016 – Curt Hill
Finally There are a few more things that we have to wait for a subsequent presentation Copyright © 2016 – Curt Hill