Dynamically building expressions
When you are creating a script to search for strings with regular expressions, the search pattern may need to change with each execution. Instead of updating your regular expressions each time you run the script, PowerShell offers the ability to dynamically build regular expressions. For example, if you needed to search files for members of an Active Directory group that frequently changed, you would need to update the expression each time you ran the script. Instead you could query the Active Directory group during runtime, and dynamically build the regular expression for searching. This provides the flexibility to change search terms on-the-fly, and doesn't require you to edit your script for each execution.
To start, you will need to declare an array of objects to feed into the regular expression. In this case, we will define "administrator"
, "password"
, and "username"
and set them in the $myarray
variable. You then continue...