Skip to content

Notes: Flow Control

Beth edited this page Sep 12, 2024 · 1 revision

Part 1: If/Else switches

Powershell if/else switches

image

  • -gt => greater than
  • -lt => less than
  • -ge => greater than or equal to
  • -le => less than or equal to

image

Powershell checking empty/null

image

String comparisons with -like

image

Wildcard Characters

      • Match zero or more characters
  • a* - Match aA, ag, and Apple
  • a* - doesn't match banana

? - Match one character in that position

  • ?n matches an, in, and on but not ran

image

[] - Match a range of characters or specific characters

  • [a-l]ook matches book, cook, and look but not took
  • [bc] matches book and cook but not hook

Regex & -match

  • -match checks if string contains regex: 'book' -match 'oo' returns true

image

-contains

  • -contains checks if a value is in a collection

image

Part 2 Arrays

Array Initialization and Count

@ sign is used to define arrays and collections

Count gives number of elements

image

image

Array Access

Strings are arrays and can be defined with Write-Output

image

image

image

image

Array -join, -contains, -in

image

[pscustomobject]

image

Part 3 Functions

Function Definition

image

Function Call

image

Dot Notaion

  • Helps call functions from other script files
  • After dot notation, the functions in the other script files can be used in the current script file