Computer Library windows 2008 exchange server power shell security register

Convert a String to Upper/Lowercase

1. Problem
You want to convert a string to uppercase or lowercase.
2. Solution
Use the ToUpper() and ToLower() methods of the string to convert it to uppercase and lowercase, respectively.
To convert a string to uppercase, use the ToUpper() method:
        PS >”Hello World”.ToUpper()
        HELLO WORLD

To convert a string to lowercase, use the ToLower() method:
        PS >”Hello World”.ToLower()
        [...]


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


Create a String

1. Problem
You want to create a variable that holds text.
2. Solution
Use PowerShell string variables to give you a way to store and work with text.
To define a string that supports variable expansion and escape characters in its definition, surround it with double quotes:
        $myString = “Hello World”

To define a literal string (that does not [...]


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


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


Simplify Most Where-Object Filters

The Where-Object cmdlet is incredibly powerful, in that it allows you to filter your output based on arbitrary criteria. For extremely simple filters (such as filtering based only on a comparison to a single property), though, the syntax can get a little ungainly:
        Get-Process | Where-Object { $_.Handles -gt 1000 }

For this type of situation, [...]


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


Get the System Date and Time

1. Problem
You want to get the system date.
2. Solution
To get the system date, run the command Get-Date.
3. Discussion
The Get-Date command generates rich object-based output, so you can use its result for many date-related tasks. For example, to determine the current day of the week:
        PS >$date = Get-Date
        PS >$date.DayOfWeek
        Sunday

For more information about the [...]


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