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 [...]
Format a Date for Output
1. Problem
You want to control the way that PowerShell displays or formats a date.
2. Solution
To control the format of a date, use one of the following options:
The Get-Date cmdlet’s –Format parameter:
PS >Get-Date -Date “05/09/1998 1:23 PM” -Format “dd-MM-yyyy @ hh:mm:ss”
09-05-1998 @ 01:23:00
PowerShell’s string formatting (–f) operator:
PS >$date = [DateTime] “05/09/1998 1:23 PM”
PS >”{0:dd-MM-yyyy @ hh:mm:ss}” [...]
Search a String for Text or a Pattern
1. Problem
You want to determine if a string contains another string, or want to find the position of a string within another string.
2. Solution
PowerShell provides several options to help you search a string for text.
Use the –like operator to determine whether a string matches a given DOS-like wildcard:
       PS >”Hello World” -like “*llo W*”
       True
[...]
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 [...]
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 [...]
Add Custom Methods and Properties to Objects
1. Problem
You have an object and want to add your own custom properties or methods (members) to that object.
2. Solution
Use the Add-Member cmdlet to add custom members to an object.
3. Discussion
The Add-Member cmdlet is extremely useful in helping you add custom members to individual objects. For example, imagine that you want to create a report [...]
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 [...]
Control Access and Scope of Variables and Other Items
1. Problem
You want to control how you define (or interact with) the visibility of variables, aliases, functions, and drives.
2. Solution
PowerShell offers several ways to access variables.
To create a variable with a specific scope, supply that scope before the variable name:
       $SCOPE:variable = value
To access a variable at a specific scope, supply that scope before [...]

