Home   Kaiming



Understanding PSSnapin

Start Windows Powershell

PS C:\> Get-PSSnapin | fl Name

Name : Microsoft.PowerShell.Core

Name : Microsoft.PowerShell.Host

Name : Microsoft.PowerShell.Management

Name : Microsoft.PowerShell.Security

Name : Microsoft.PowerShell.Utility

Start Exchange Management Shell

[PS] C:\>Get-PSSnapin | fl name

Name : Microsoft.PowerShell.Core

Name : Microsoft.PowerShell.Host

Name : Microsoft.PowerShell.Management

Name : Microsoft.PowerShell.Security

Name : Microsoft.PowerShell.Utility

Name : Microsoft.Exchange.Management.PowerShell.Admin

power01

When you load the Microsoft.Exchange.Management.PowerShell.Admin into Windows PowerShell, it becomes Exchange Management Shell.

Exchange Management Shell actually load the C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'"

From Windows PowerShell windows, running Exchange.ps1 script will create the same environment as Exchange Management Shell.

power02

power03

Run AllMailbox2.ps1 from command line:

power04

The extension of a powershell script is ps1.


Customizing your PowerShell environment

Profile

Where and what filename?

In Windows PowerShell window, type $Profile variable.

2003 computer:

[PS] C:\>$profile
C:\Documents and Settings\kaiming\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

2008 computer:

[PS] C:\>$profile
C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

You have to create WindowsPowerShell subfolder first.

power05

Start Exchange Management Shell;

power06


Wildcard *

Get-Help *database*
Get-Help *server*
Get-Help *user*

[PS] C:\>Get-Help Set-Mailbox -Parameter *Quota*

Tab-completion

You can partially enter a command and then hit the tab key to have PowerShell try to fill in the rest of the command.

SHIFT-TAB to go back to previous parameter

Get-Mailbox –identity Bond | Format-List


Variables

Common automatic variables

$_

It represents the current pipeline object that is used in script blocks, filters, and the Where statement.

PowerShell07

Retrieve All mailboxes under Ex20071\First Storage Group

Get-MailboxDatabase -Server Ex20071 | Where {$_.StorageGroup.Name -eq 'First Storage Group'} | ForEach-Object {Get-Mailbox -Database $_.Name} | Select Name, Alias, Database

Power08

Create a batch file: safeSenderList.bat

Powershell.exe Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
get-mailbox | where {$_.recipientTypeDetail -eq "UserMailbox"} | update-safelist

power09

From Windows Command Prompt, you can run safeSenderList.bat.


User defined variables and Foreach vs. Foreach-object

$Forest=[System.DirectoryServices.ActiveDirectory.Forest]::GetcurrentForest()

$GClist = $Forest.FindAllGlobalCatalogs()

Foreach($o in $GCList) { $o.name; }

$GClist | Foreach-object { $_.name}

power10

power11

Example: create a mailbox

# Your password must meet the requirement of your domain

$password=read-host "Enter password please!" -asSecureString

$domain="work"
$server="test1"

$firstName="George"
$lastName="Allistair"
$company="house"


New-Mailbox -Name "$firstName $lastName" -Alias "$firstName.$lastName" -OrganizationalUnit "$domain.com/$company OU" -UserPrincipalName "$firstName.$lastName@$company.com" -SamAccountName "$firstName.$lastName" -FirstName $firstName -initials "" -LastName $lastName -Password $password -Database "$server\First Storage Group\$company"


Set-User -Identity "$firstName.$lastname" -Company $company

Script: change the display name to FirstName,LastName format by foreach

$Users=Get-User -Filter "(RecipientTypeDetails -eq 'UserMailbox') -or (RecipientTypeDetails -eq 'LegacyMailBox')"
ForEach($U in $Users)
{
$Mailbox = $U.Name
$DN = $U.FirstName + ", " +$U.LastName
Write-Host "Setting display name for mailbox " $DN
Set-User $Mailbox -DisplayName $DN
$DN = $Null
}

Script:change the display name to LastName,FirstName format by foreach-object

$Users=Get-User -Filter "(RecipientTypeDetails -eq 'UserMailbox') -or (RecipientTypeDetails -eq 'LegacyMailBox')"
$Users | foreach-object { Set-User -id $_.Name -DisplayName ($_.LastName+","+$_.FirstName)}


Other system variables

$exScript

This variable the full path of the Exchange scripts directory.

$exBin

This variable displays the full path of the Exchange Server\bin directory.

$ExInstall

This variable displays the full path of the Exchange Server \Exchange Server directory.

$AdminSessionADSettings

$AdminSessionADSettings.ViewEntireForest = $True
$AdminSessionADSettings.PreferredGlobalCatalog = "GC1.work.com"

$AdminSessionADSettings.DefaultScope = "work.com/House OU"
$AdminSessionADSettings.PreferredDomainControllers = "DC1.work.com"

 


Home