Computer Library windows 2008 exchange server power shell security register

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}” [...]


Work with Each Item in a List or Command Output

1. Problem
You have a list of items and want to work with each item in that list.
2. Solution
Use the Foreach-Object cmdlet (which has the standard aliases foreach and %)to work with each item in a list.
To apply a calculation to each item in a list, use the $_ variable as part of a calculation in [...]


Filter Items in a List or Command Output

Recipe 2.1. Filter Items in a List or Command Output
1. Problem
You want to filter the items in a list or command output.
2. Solution
Use the Where-Object cmdlet (which has the standard aliases, where and ?) to select items in a list (or command output) that match a condition you provide.
To list all running processes that have [...]


Configure Debug, Verbose, and Progress Output

1. Problem
You want to manage the detailed debug, verbose, and progress output generated by cmdlets and scripts.
2. Solution
To enable debug output for scripts and cmdlets that generate it:
       
        $debugPreference = “Continue”
        Start-DebugCommand

To enable verbose mode for a cmdlet that checks for the -Verbose parameter:
        Copy-Item c:\temp\*.txt c:\temp\backup\ -Verbose

To disable progress output from a script [...]


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
        [...]