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}” [...]
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 [...]
Use a COM Object
1. Problem
You want to create a COM object to interact with its methods and properties.
2. Solution
Use the New-Object cmdlet (with the –ComObject parameter) to create a COM object from its ProgID. You can then interact with the methods and properties of the COM object as you would any other object in PowerShell.
       $object = New-Object [...]
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 [...]
Manage the Error Output of Commands
1. Problem
You want to display detailed information about errors that come from commands.
2. Solution
To list all errors (up to $MaximumErrorCount) that have occurred in this session, access the $error array:
       $error
To list the last error that occurred in this session, access the first element in the $error array:
       $error[0]
To list detailed information about an [...]
Store the Output of a Command into a File
1. Problem
You want to redirect the output of a pipeline into a file.
2. Solution
To redirect the output of a command into a file, use either the Out-File cmdlet or one of the redirection operators.
Out-File:
      Â
       Get-ChildItem | Out-File unicodeFile.txt
       Get-Content filename.cs | Out-File -Encoding ASCII file.txt
       Get-ChildItem | Out-File-Width 120 unicodeFile.cs
Redirection operators:
       Get-ChildItem > files.txt
       [...]

