Convert Numbers Between Bases
1. Problem
You want to convert a number to a different base.
2. Solution
The PowerShell scripting language allows you to enter both decimal and hexadecimal numbers directly. It does not natively support other number bases, but its support for interaction with the .NET Framework enables conversion both to and from binary, octal, decimal, and hexadecimal.
To convert a [...]
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
[...]
Insert Dynamic Information in a String
1. Problem
You want to place dynamic information (such as the value of another variable) in a string.
2. Solution
In an expanding string, include the name of a variable in the string to insert the value of that variable.
       PS >$header = “Report for Today”
       PS >$myString = “$header’n—————-”
       PS >$myString
       Report for Today
       —————-
To include [...]
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 [...]
Adjust Script Flow Using Conditional Statements
1. Problem
You want to control the conditions under which PowerShell executes commands or portions of your script.
2. Solution
Use PowerShell’s if, elseif, and else conditional statements to control the flow of execution in your script.
For example:
Code View: Scroll / Show All
       $temperature = 90
      Â
       if($temperature -le 0)
       {
         “Balmy Canadian Summer”
       }
       elseif($temperature -le [...]
Make Decisions with Comparison and Logical Operators
1. Problem
You want to compare some data with other data and make a decision based on that comparison.
2. Solution
Use PowerShell’s logical operators to compare pieces of data and make decisions based on them.
Comparison operators:
-eq, -ne, -ge, -gt, -lt, -le, -like, -notlike, -match, -notmatch, -contains, -notcontains, -is, -isnot
Logical operators:
-and, -or, -xor, -not
For a detailed [...]
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 [...]
Automate Data-Intensive Tasks
1. Problem
You want to invoke a simple task on large amounts of data.
2. Solution
If only one piece of data changes (such as a server name or user name), store the data in a text file. Use the Get-Content cmdlet to retrieve the items, and then use the Foreach-Object cmdlet (which has the standard aliases foreach [...]

