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


Create a Multiline or Formatted String

1. Problem
You want to create a variable that holds text with newlines or other explicit formatting.
2. Solution
Use a PowerShell here string to store and work with text that includes newlines and other formatting information.
        $myString = @”
        This is the first line
        of a very long string. A “here string”
        lets you to create blocks [...]


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


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


Invoke a PowerShell Script From Outside PowerShell

1. Problem
You want to invoke a PowerShell script from a batch file, a logon script, scheduled task, or any other non-PowerShell application.
2. Solution
Launch PowerShell.exe in the following way:
        PowerShell “& ‘full path to script’ arguments”

For example,
        PowerShell “& ‘c:\shared scripts\Get-Report.ps1′ Hello World”

3. Discussion
Supplying a single string argument to PowerShell.exe invokes PowerShell, runs the command as [...]