Download presentation
Presentation is loading. Please wait.
Published byFalko Braun Modified over 5 years ago
1
PowerShell Flow of Control Copyright © 2016 – Curt Hill
2
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
3
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
4
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
5
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
6
Logical Operators We also have Boolean operators: Not is –not
And is –and Or is –or Exclusive or is –xor Copyright © 2016 – Curt Hill
7
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
8
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
9
Example Copyright © 2016 – Curt Hill
10
Decisions The language contains several flow statements If Switch For
While Do foreach Copyright © 2016 – Curt Hill
11
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
12
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
13
Examples Copyright © 2016 – Curt Hill
14
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
15
Switch example Clear enough? Copyright © 2016 – Curt Hill
16
For loops Very similar to that of C with the exception of variable names and conditionals See the next slide Copyright © 2016 – Curt Hill
17
For example Copyright © 2016 – Curt Hill
18
Whiles Similarly the while and do while look like those of C
Copyright © 2016 – Curt Hill
19
While Example Copyright © 2016 – Curt Hill
20
Do While Example Copyright © 2016 – Curt Hill
21
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
22
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
23
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
24
Example Copyright © 2016 – Curt Hill
25
Finally There are a few more things that we have to wait for a subsequent presentation Copyright © 2016 – Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.