Lessons In PowerShell - Escape Sequences

   Submit to Reddit      
  

The following useful information is primarily taken from the Windows PowerShell Language Quick Reference (QuadFold.rtf) documentation.

Escape Sequences

The escape character in Windows PowerShell is the backwards apostrophe.

Escape Sequence Description
`0 (null)
`a (alert)
`b (backspace)
`f (form feed)
`n (new line)
`r (carriage return)
`t (tab)
`v (vertical quote)

Lessons In PowerShell - Loops

   Submit to Reddit      
  

The following useful information is primarily taken from the Windows PowerShell Language Quick Reference (QuadFold.rtf) documentation.

For

Format:

[:label] for ([initializer]; [condition]; [iterator]) {}

Example:

for ($i=0; $i -lt 5; $i++) { Write-Object $i }

For Each

Format:

[:label] 
foreach (identifier in collection) {}

Expression | foreach {}

Expression | foreach {BEGIN{} PROCESS{} END{}}

Examples:

$i = 1,2,3
foreach ($z in $i) { Write-Object $z } 

Get-Process | foreach {
    BEGIN{$x=1}
    PROCESS{$X++} 
    END{"$X Processes"}
}

Do Until

do
{ 
    ...
} until (condition)

While

[:label] while (condition)
{
    ...
}

Do While

do
{ 
    ...
} while (condition)

Lessons In PowerShell - Conditions and Operators

   Submit to Reddit      
  

The following useful information is primarily taken from the Windows PowerShell Language Quick Reference (QuadFold.rtf) documentation.

If / Elseif / Else

When executed on the command line, the closing brace must be on the same line as the preceding keyword.

if (condition) {...} 
elseif (condition) {...} 
else {...}</blockquote>

Switch

The variable $_ is available in the script. $_ represents the current value being evaluated. If an array is used in switch, each element of the array is tested.

Example:

$var = "word1","word2","word3"
switch -regex ($var) {
  "word1" {"Multi-match Exact " + $_ }
  "word2" {"Multi-match Exact " + $_ }
  "w.*2" {"Pattern match Exact " + $_ }
  default {"Multi-match Default " + $_ }
}

Output:

Multi-match Exact word1
Multi-match Exact word2
Pattern match Exact word2
Multi-match Default word3

Comparison Operators

The following operators can be used in the condition expressions.

Operator Description Example (true)
-eq Equal 1 -eq 1
-ne Not equal 1 -ne 2
-gt -ge Greater than, greater than or equal to 2 -gt 1
1 -ge 1
-lt -le Less than, less than or equal to 1 -lt 2
1 -le 1
-like Like - for text using wildcards "abc.efg" -like "?bc.*"
-notlike Not Like "abc.efg" -notlike "b*"
-contains Contains 1,2,3 -contains 1
-notcontains Not Contains 1,2,3 -notcontains 4

Prepend "i" for a case-insensitive operation. (e.g. -ieq) Prepend "c" for a case-sensitive operation. (e.g. -ceq)

Logical Operators

The following operators can be used for logical operations.

Operator Description Example (true)
-and Logical AND (1 -eq 1) -and (1 -ne 2)
-or Logical OR (1 -eq 1) -or ($FALSE)
-not or ! Logical NOT -not ($FALSE)

Binary Operators

The following operators can be used for binary operations.

Operator Description
-band Binary AND
-bor Binary OR
-bnot Binary NOT

String Operators

The following operators can be used on strings.

Operator Description
+ Concatenate two strings
* Repeat a string some number of times
-f Format a string (.NET format specifiers)
-replace Replace operator
"abcd" -replace "bc", "TEST"
aTESTd
-match Regular expression match
-like Wildcard matching

Type Operators

The following operators can be used on types.

Operator Description Example (true)
-is Type evaluator $a -is [string]
-as Type converter 100 -as [string]

Command Expansion Operators

The following operators can be used for command expansion.

Operator Description
$( ) Returns null
$(1,2,3) Returns an array containing 1,2,3.
$(Get-Alias a*) Returns evaluation of the expression
@(Get-Alias;Get-Process) Executes the two commands and returns the results in an array

Other Operators

Operator Description Example (true)
, Array constructor
.. Range operator
& Invoke a script block or the name of a command or function $a = "Get-Process"
&$a
$a = { Get-Process | Select -First 2 }
&$a

Boolean Expressions

The following expressions will result in a boolean value of $TRUE or $FALSE.

TRUE FALSE
Any string of length > 0 except the word "false" Empty string or the string "false"
Any number !=0 Any number = 0
Array of length > 1 Array of length 0
Array of length 1 whose element is TRUE Array of length 1 whose element is FALSE
A reference to any object Null

SOLID Principles

   Submit to Reddit      
  

In a previous posting on behaviour-driven programming, I quickly referenced 'Uncle Bob' Robert C. Martin's website where he outlines the SOLID Principles.

I wanted to expand upon that and provide a few more helpful links.

The best examples I have seen that demonstrates these principles can be found in a series of short videos on DimeCasts.net.

Scott Hanselman too has a number of podcasts where he is directly interviewing 'Uncle Bob': Hanselminutes Podcast 145 - SOLID Principles with Uncle Bob - Robert C. Martin

When I listened to this podcast, I was quite pleased to hear and have my personal experience validated when 'Uncle Bob' rated the Single Responsibility Principle as the most important. In my personal experience, and in the experience of a number of my colleagues, this principle alone has had the largest positive effect in the quest to write cleaner and more intuitive code.

Hanselminutes Podcast 150 - Uncle Bob Martin, this time with feeling

And a more recent podcast discussing the related topic of professionalism.

Hanselminutes Podcast 171 - The Return of Uncle Bob