Simplify Math with Administrative Constants
1. Problem
You want to work with common administrative numbers (that is, kilobytes, megabytes, and gigabytes) without having to remember or calculate those numbers.
2. Solution
Use PowerShell’s administrative constants (KB, MB, and GB) to help work with these common numbers.
Calculate the download time (in seconds) of a 10.18 megabyte file over a connection that gets 215 kilobytes [...]
Measure Statistical Properties of a List
1. Problem
You want to measure the numeric (minimum, maximum, sum, average) or textual (characters, words, lines) features of a list of objects.
2. Solution
Use the Measure-Object cmdlet to measure these statistical properties of a list.
To measure the numeric features of a stream of objects, pipe those objects to the Measure-Object cmdlet:
       PS >1..10 | Measure-Object–Average -Sum
       [...]
Perform Complex Arithmetic
1. Problem
You want to use PowerShell to calculate more complex or advanced mathematical results.
2. Solution
PowerShell supports more advanced mathematical tasks primarily through its support for the System.Math class in the .NET Framework.
To find the absolute value of a number, use the [Math]::Abs() method:
       PS >[Math]::Abs(-10.6)
       10.6
To find the power (such as the square or the [...]
Convert a String to Upper/Lowercase
1. Problem
You want to convert a string to uppercase or lowercase.
2. Solution
Use the ToUpper() and ToLower() methods of the string to convert it to uppercase and lowercase, respectively.
To convert a string to uppercase, use the ToUpper() method:
       PS >”Hello World”.ToUpper()
       HELLO WORLD
To convert a string to lowercase, use the ToLower() method:
       PS >”Hello World”.ToLower()
       [...]
Place Special Characters in a String
1. Problem
You want to place special characters (such as tab and newline) in a string variable.
2. Solution
In an expanding string, use PowerShell’s escape sequences to include special characters such as tab and newline.
       PS >$myString = “Report for Today’n—————-”
       PS >$myString
       Report for Today
       —————-
3. Discussion
As discussed in Section 5.1, “Create a String,” PowerShell strings [...]
Create a String
1. Problem
You want to create a variable that holds text.
2. Solution
Use PowerShell string variables to give you a way to store and work with text.
To define a string that supports variable expansion and escape characters in its definition, surround it with double quotes:
       $myString = “Hello World”
To define a literal string (that does not [...]
Add a Pause or Delay
1. Problem
You want to pause or delay your script or command.
2. Solution
To pause until the user presses ENTER, use the Read-Host cmdlet:
       PS >Read-Host “Press ENTER”
       Press ENTER:
To pause until the user presses a key, use the ReadKey() method on the $host object:
       PS >$host.UI.RawUI.ReadKey()
To pause a script for a given amount of [...]
Repeat Operations with Loops
1. Problem
You want to execute the same block of code more than once.
2. Solution
Use one of PowerShell’s looping statements (for, foreach, while, and do), or PowerShell’s Foreach-Object cmdlet to run a command or script block more than once. For a detailed description of these looping statements, see “Looping Statements” in . For example:
Code View: Scroll [...]
Manage Large Conditional Statements with Switches
1. Problem
You want to find an easier or more compact way to represent a large if … elseif … else conditional statement.
2. Solution
Use PowerShell’s switch statement to more easily represent a large if … elseif … else conditional statement.
For example:
       $temperature = 20
Â
       switch($temperature)
       {
         { $_ -lt 32 }  { “Below Freezing”; break [...]
Types and Objects
1. Problem
You have an instance of an object and want to know what methods and properties it supports.
2. Solution
The most common way to explore the methods and properties supported by an object is through the Get-Member cmdlet.
To get the instance members of an object you’ve stored in the $object variable, pipe it to the Get-Member [...]

