Powershell

Table of Contents

Strings, Arrays and Regex

Full Regex

A regular expression (regex)is an pattern used to match text that can be made up of literal characters, operators, and other constructs. Certain characters have special meaning within a regex, and they enable you to detect patterns of variable text. Regex's have existed since the time of their implementation in UNIX operating systems created by An mathematician to find patterns within a string. These patterns can derive for finding an IP address, in a CMD/PowerShell "ipconfig /all" output, to finding a certain keyword in every file in an entire file drive and subdirectories. Regex characters in PowerShell are adhered to Just like their UNIX counterparts in common Linux Operating Systems, and running an UNIX Regex pattern search will work in most cases in newer Windows OS.
PowerShell uses the below commands and switches to search for patterns.

List of Regex used in PowerShell.

\w
matches “word characters,” which means letters, numbers, and underscores, but no punctuation and no whitespace. The regex \won would match Don, Ron, and ton, with the \w standing in for any single letter, number, or underscore.

\W
matches the opposite of \w (so this is one example where PowerShell is sensitive to case), meaning it matches whitespace and punctuation—“nonword characters,” in the parlance.

\d
matches any digit from 0 through 9 inclusive.

\D
matches any nondigit.

\s
matches any whitespace character, including a tab, space, or carriage return.

\S
matches any nonwhitespace character.

.
(a period) stands in for any single character.

[abcde]
matches any character in that set. The regex d[aeiou]n would match don and dan, but not doun or deen.

[a-z]
matches one or more characters in that range. You can specify multiple ranges as comma-separated lists, such as [a-f,m-z].

[^abcde]
matches one or more characters that are not in that set, meaning the regex d[^aeiou] would match dns but not don.

?
follows another literal or special character and matches exactly one instance of that character. So, the regex do?n would match don but would not match doon. It would also match dn because ? can also match zero instances of the preceding character.

*
matches any number of instances of the preceding character. The regex do*n would match both doon and don. It would also match dn because * also matches zero instances of the preceding character.

+
matches one or more instances of the preceding character. You’ll see this used a lot with parentheses, which create a sort of subexpression. For example, the regex (dn)+o would match dndndndno because it matches repeating instances of the dn subexpression.

\
(backslash) is the regex escape character. Use it in front of a character that normally has special meaning in the regex syntax, to make that character a literal. For example, the regex \. would match a literal period character, rather than allowing the period to stand in for any single character, as it normally does. To match a literal backslash, escape it with a backslash: \\.

{2}
matches exactly that many instances of the preceding character. For example, \d{1} matches exactly one digit. Use {2,} to match two or more, and use {1,3} to match at least one, but no more than three.

^
matches the beginning of the string. For example, the regex d.n would match don as well as pteranodon. But the regex ^d.n would match don but would not match pteranodon because the ^ makes the matching occur at the beginning of the string. This is a different use of ^ than in the previous example, where it was used with square brackets, [], to indicate a negative match.

$
matches the end of the string. For example, the regex .icks would match hicks and sticks (the match would technically be on ticks in that example), and would also match Dickson. But the regex .icks$ would not match Dickson because the $ indicates that the string should reach its end after the s. There you have it—a whirlwind look at the basic regex syntax. As we wrote earlier, there’s a lot more where that came from, but this is enough to get some basic work done. Let’s look at some example regular expressions:

\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
matches the pattern of an IPv4 address, although it’ll accept illegal data like 432.567.875.000 as well as legal data like 192.169.15.12.

\\\\\w+(\\\w+)+
matches a Universal Naming Convention (UNC) path. All the backslashes make that regex hard to read—which is one reason it’s important to test and tweak your regular expressions before you rely on them in a production task.

\w{1}\.\w+@poshpython\.com
matches a specific type of email address: first initial, a period, last name, and then @poshpython.com. For example, sumeet.singh@poshpython
.com would be a valid match. You do have to be a bit careful with these. For example, donald.jones@company.com.org would also be a valid match. The regex is fine with there being extra text before and after the matched portion. That’s where the ^ and $ anchors come into play in many situations.

Operators
As well as Regex, PowerShell uses its own set of switch Operators to match patterns as per below.

$array = 'ABC','DEF','GHI'
$array -match 'DEF'


-eq
Equality (-ceq for case-sensitive string comparisons)

-ne
Inequality (-cne for case-sensitive string comparisons)

-ge
Greater than or equal to (-cge for case-sensitive string comparisons)

-le
Less than or equal to (-cle for case-sensitive string comparisons)

-gt
Greater than (-cgt for case-sensitive string comparisons)

-lt
Less than (-clt for case-sensitive string comparisons)

-contains
Returns True if the specified collection contains the object specified ($collection -contains $object); -notcontains is the reverse.

-in
Returns True if the specified object is in the specified collection ($object -in $collection); -notin is the reverse.

To combine multiple pattern comparisons, PowerShell uses the below Logical Operators

-not
Reverses True and False (the ! symbol is an alias for this operator)

-and
Both subexpressions must be True for the entire expression to be True

-or
Either subexpression can be True for the entire expression to be True In addition, there are operators that perform specific functions:

-join
Joins the elements of an array into a delimited string

-split
Splits a delimited string into an array

-replace
Replaces occurrences of one string with another

-is
Returns True if an item is of the specified type ($one -is [int])

-as
Casts the item as the specified type ($one -as [int])

..
Is a range operator; 1..10 returns ten objects, 1 through 10

-f
Is the format operator, replacing placeholders with values: "{0}, {1}" -f "Hello","World"

Common Examples of Operators in action

HTML Stripping

Foreach-object {$_ -replace '<[^>]+>' ,''}


or the below pure Regex match expression, both achieve same result

Foreach-object {$_ -replace '<[^>]+>' ,''}


String Formatting to 2 decimal places

"{0:N2}" -f ($drive.capacity/1GB


Visit next page to learn about - WMI