Computer Library windows 2008 exchange server power shell security register

Place Special Characters in a String

1. Problem
You want to place special characters (such as tab and newline) in a string variable.
2. Solution
In an expanding string, use PowerShell’s escape sequences to include special characters such as tab and newline.
        PS >$myString = “Report for Today’n—————-”
        PS >$myString
        Report for Today
        —————-

3. Discussion
As discussed in Section 5.1, “Create a String,” PowerShell strings [...]


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


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


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


Display the Properties of an Item As a List

1. Problem
You have an item (for example, an error record, directory item, or .NET object), and you want to display detailed information about that object in a list format.
2. Solution
To display detailed information about an item, pass that item to the Format-List cmdlet. For example, to display an error in list format, type the commands:
        [...]


Access and Manage Your Console History

1. Problem
After working in the shell for a while, you want to invoke commands from your history, view your command history, and save your command history.
2. Solution
The shortcuts given in Section 1.12, “Customize the Shell to Improve Your Productivity” let you manage your history, but PowerShell offers several features to help you work with your [...]


Measure the Duration of a Command

1. Problem
You want to know how long a command takes to execute.
2. Solution
To measure the duration of a command, use the Measure-Command cmdlet:
        PS >Measure-Command { Start-Sleep -Milliseconds 337 }

        Days              : 0
        Hours             : 0
        Minutes           : 0
        Seconds           : 0
        Milliseconds      : 339
        Ticks             : 3392297
        TotalDays         : 3.92626967592593E-06
        TotalHours        : 9.42304722222222E-05
        TotalMinutes      [...]


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


Get Help on a Command

1. Problem
You want to learn about how a specific command works and how to use it.
2. Solution
The command that provides help and usage information about a command is called Get-Help. It supports several different views of the help information, depending on your needs.
To get the summary of help information for a specific command, provide the [...]


Find a Command to Accomplish a Task

1. Problem
You want to accomplish a task in PowerShell but don’t know the command or cmdlet to accomplish that task.
2. Solution
Use the Get-Command cmdlet to search for and investigate commands.
To get the summary information about a specific command, specify the command name as an argument:
        Get-Command CommandName

To get the detailed information about a specific command, [...]