Get-Content
Create a text file "Users.txt"
Algo Clinton
Amie Baldwin
George Allistair
Administrator
Rudy
King
(Get-Content .\users.txt) | Get-MailboxStatistics
(Get-Content .\users.txt) | Set-Mailbox -CustomAttribute1 VIP
(Get-Content
.\users.txt) | get-mailbox |ft name,CustomAttribute1
$? variable
It is a Boolean variable that represents the success or failure of the last cmdlet.
Get-ExchangeServer (Get-Content env:computerName)
$?
Assuming the directory F:\APower is created.
new-item -Path f:\apower -type file -ErrorAction SilentlyContinue |
Out-Null
if(!$?){write-host "It exists"}
When you create a script, the $? is quite handy. The -ErrorAction SilentlyContinue will suppress the interactive response.
Active Directory Module for Active Directory
The Active Directory module is available in the following editions of Windows Server 2008 R2 and Windows 7:
Windows Server 2008 R2 Standard
Windows Server 2008 R2 Enterprise
Windows Server 2008 R2 Datacenter
Windows 7
To manually load Active Directory module in PowerShell in Windows Server 2008 R2 domain member:
Import-module serverManager
Add-WindowsFeature -Name RSAT-AD-PowerShell
-IncludeAllSubFeature
import-module ActiveDirectory
In Windows 7, download Administration Tools package at http://go.microsoft.com/fwlink/?LinkID=137379
Control Panel---Programs---Programs and Features --Turn Windows Features on or off ---Remote Server Administration Tools
Select AD DS and AD LDS Tools---Active Directory Module for Windows PowerShell
Start PowerShell
Import-module ActiveDirectory
Define an Array
$a=@()
$a.gettype()
| IsPublic | IsSerial | Name | BaseType |
| True | True | Object[] | System.Array |
Example: report of user mailboxes
$ExchangeServer=Get-ExchangeServer
$AllUsers=@()
ForEach ($Server in
$ExchangeServer){$AllUsers +=Get-Mailbox -Server $Server | Get-MailboxStatistics
|Select ServerName,DisplayName,ItemCount,TotalItemSize}
$AllUsers | Export-CSV c:\MailboxList.CSV
-NoTypeInformation
Example:Looking for large folders
get-mailbox | Get-MailboxFolderStatistics | where{$_.ItemsInFolder -GT 500} |Sort ItemsInfolder -descending | ft Identity,ItemsInFolder
| Identity | ItemsInFolder |
| lab.com/min kuang\Sent Items\z 2009 inbox | 26866 |
| lab.com/min kuang\Inbox | 17287 |
| lab.com/asifund/Julia\Inbox | 6561 |
| lab.com/min kuang\Sent Items | 5551 |
Example: creating a report in HTML
Get-MailboxDataBase | Get-mailboxStatistics | Sort StorageLimitStatus,TotalItemSize -Descending | ConvertTo-HTML DisplayName,StorageLimitStatus,Database,ItemCount,TotalItemSize >C:\mailbox.html
Disconnected Mailbox
If you remove a mailbox, the mailbox data stored in the Exchange mailbox database is marked for deletion, and the associated user account is also deleted from Active Directory. By default, Exchange retains a disconnected mailbox for 30 days.
To retain the user account and disassociate only the mailbox data from the user account, you must disable the mailbox.
After a mailbox is removed and before it is deleted from Exchange database, it is listed as Disconnected Mailbox. After a mailbox is disabled, it is listed as Disconnected Mailbox.
get-mailboxserver | Get-mailboxStatistics |Where {$_.DisconnectDate -ne
$Null} | ft DisplayName,OriginatingServer
Send mail
$SmtpClient=New-Object
System.Net.Mail.SmtpClient
$Smtpclient.Host='SMTP.Telus.Net'
$From='Kaiming.Liao@GMAIL.COM'
$To='kmliao@hotmail.com'
$Title='This
is a test mail.'
$Body='How are you
doing?'
$SmtpClient.Send($From,$To,$Title,$Body)
Fetch credential for SMTP authentication
Using clear password
$credential=new-object
System.Net.NetworkCredential('KMLIAO@hotmail.com','Password')
$SmtpClient.Credentials=$credential
$credential.UserName
KMLIAO@hotmail.com
$credential.Password
password
Using a standard login window
$credential=get-credential
$Username=$credential.UserName
$Password=$credential.GetNetworkCredential().Password
$credential1=New-Object
System.Net.NetworkCredential($Username,$Password)
$SmtpClient.Credentials=$credential1
Reporting database size and mailbox count via email
$db=Get-MailboxDatabase -server Ex1
foreach
($objItem in $db){$objItem.edbfilepath}
IsPathInRootDirectory : False
PathName : C:\Program
Files\Microsoft\Exchange Server\V14\Mailbox\Mailbox Database 2016968937\Mailbox
Data
base 2016968937.edb
IsLocalFull : True
IsUnc : False
DriveName
: C:
ServerName :
IsPathInRootDirectory : False
PathName : C:\Program
Files\Microsoft\Exchange Server\V14\Mailbox\Vanarts\Vanarts.edb
IsLocalFull :
True
IsUnc : False
DriveName : C:
ServerName :
IsPathInRootDirectory : False
PathName : C:\Program
Files\Microsoft\Exchange
Server\V14\Mailbox\TelusDataBase\TelusDataBase.edb
IsLocalFull :
True
IsUnc : False
DriveName : C:
ServerName :
IsPathInRootDirectory : False
PathName : C:\Program
Files\Microsoft\Exchange
Server\V14\Mailbox\ShawDataBase\ShawDataBase.edb
IsLocalFull : True
IsUnc
: False
DriveName : C:
ServerName :
$db=Get-MailboxDatabase
"Shawdatabase"
$db.EdbFilePath
IsPathInRootDirectory : False
PathName : C:\Program
Files\Microsoft\Exchange
Server\V14\Mailbox\ShawDataBase\ShawDataBase.edb
IsLocalFull : True
IsUnc
: False
DriveName : C:
ServerName :
$db.EdbFilePath.DriveName
C:
$db.EdbFilePath.DriveName.Remove(1)
C
$db.EdbFilePath.PathName
C:\Program
Files\Microsoft\Exchange
Server\V14\Mailbox\ShawDataBase\ShawDataBase.edb
$db.EdbFilePath.PathName.Remove(0,2)
\Program
Files\Microsoft\Exchange Server\V14\Mailbox\ShawDataBase\ShawDataBase.edb
$Server="Ex1"
$path="\\" + $Server +"\" +
$db.EdbFilePath.Drivename.Remove(1).ToString()+"$"+$db.EdbFilePath.PathName.Remove(0,2)
$path
\\Ex1\C$\Program
Files\Microsoft\Exchange
Server\V14\Mailbox\ShawDataBase\ShawDataBase.edb
$db=Get-MailboxDatabase
"Shawdatabase"
$edbfilepath=$db.edbfilepath
$Server="Ex1"
$path="\\" +
$Server +"\" +
$db.EdbFilePath.Drivename.Remove(1).ToString()+"$"+$db.EdbFilePath.PathName.Remove(0,2)
$dbsize=Get-ChildItem
$path
$EdbFilePath.PathName.Remove(0,2)
\Program Files\Microsoft\Exchange Server\V14\Mailbox\ShawDataBase\ShawDataBase.edb
$EdbFilePath.PathName.Length-6
$dbpath=$EdbFilePath.PathName.Remove(0,2).Remove($EdbFilePath.PathName.Length-6)
$dbpath
\Program Files\Microsoft\Exchange Server\V14\Mailbox\ShawDataBase\ShawDataBase

$db=Get-MailboxDatabase
"Shawdatabase"
$edbfilepath=$db.edbfilepath
$Server="Ex1"
$path="\\" +
$Server +"\" +
$db.EdbFilePath.Drivename.Remove(1).ToString()+"$"+$db.EdbFilePath.PathName.Remove(0,2)
$dbsize=Get-ChildItem
$path
$EdbFilePath.PathName.Remove(0,2)
$EdbFilePath.PathName.Length-6
$dbpath=$EdbFilePath.PathName.Remove(0,2).Remove($EdbFilePath.PathName.Length-6)
$dbpath
$mailboxcount=Get-mailboxStatistics
-Database $db | Measure-Object
$mailboxcount
Count : 2
Average :
Sum :
Maximum :
Minimum
:
Property :
Script file:Data.ps1
$BodyText=""
$ExchangeServer=Get-ExchangeServer | Where
{$_.AdminDisplayVersion.major -eq 14 -and $_.IsMailboxServer -eq $True}
ForEach ($Server in $ExchangeServer)
{
$db=Get-MailboxDatabase -Server
$Server
ForEach ($objItem in
$db)
{
$edbfilepath=$objItem.edbfilepath
$path="\\" + $Server +"\"
+$objItem.EdbFilePath.Drivename.Remove(1).ToString()+"$"+
$objItem.EdbFilePath.PathName.Remove(0,2)
$dbsize=Get-ChildItem
$path
$dbpath=$objItem.EdbFilePath.PathName.Remove(0,2).Remove($EdbFilePath.PathName.Length-6)
$mailboxcount=Get-mailboxStatistics
-Database $objItem | Measure-Object
$ReturnedObj=New-Object
PSObject
$ReturnedObj | Add-Member NoteProperty -Name "Server" -Value
$Server
$ReturnedObj | Add-Member NoteProperty -Name "Database" -Value
$objItem.Identity
$ReturnedObj | Add-Member NoteProperty -Name "Size (MB)"
-Value ("{0:n2}" -f($dbsize.Length/1024KB))
$ReturnedObj | Add-Member
NoteProperty -Name "Mailbox count" -Value $mailboxcount.count
$BodyText +=
$ReturnedObj
$BodyText +="<p>"
}
}
Run the script in EMS
data.ps1
@{Server=EX1; Database=Mailbox
Database 2016968937; Size (MB)=168.06; Mailbox count=10}<br />@{Server=EX1;Database=Vanarts;
Size (MB)=136.06; Mailbox count=15}<br />@{Server=EX1; Database=TelusDataBase; Size
(MB)=136.06; Mailbox count=7}<br />@{Server=EX1; Database=ShawDataBase; Size (MB)=8.06; Mailbox
count=2}<<br
/>
script: databaseSize.ps1
$BodyText=""
$ExchangeServer=Get-ExchangeServer | Where
{$_.AdminDisplayVersion.major -eq 14 -and $_.IsMailboxServer -eq $True}
ForEach ($Server in $ExchangeServer)
{
$db=Get-MailboxDatabase -Server
$Server
ForEach ($objItem in
$db)
{
$edbfilepath=$objItem.edbfilepath
$path="\\" + $Server +"\"
+$objItem.EdbFilePath.Drivename.Remove(1).ToString()+"$"+
$objItem.EdbFilePath.PathName.Remove(0,2)
$dbsize=Get-ChildItem
$path
$dbpath=$objItem.EdbFilePath.PathName.Remove(0,2).Remove($EdbFilePath.PathName.Length-6)
$mailboxcount=Get-mailboxStatistics
-Database $objItem | Measure-Object
$ReturnedObj=New-Object
PSObject
$ReturnedObj | Add-Member NoteProperty -Name "Server" -Value
$Server
$ReturnedObj | Add-Member NoteProperty -Name "Database" -Value
$objItem.Identity
$ReturnedObj | Add-Member NoteProperty -Name "Size (MB)"
-Value ("{0:n2}" -f($dbsize.Length/1024KB))
$ReturnedObj | Add-Member
NoteProperty -Name "Mailbox count" -Value $mailboxcount.count
$BodyText +=
$ReturnedObj
$BodyText +="<br
/>"
}
}
$BodyText=$BodyText.Replace("@{","")
$BodyText=$BodyText.Replace("}","")
$MailMessage
= New-Object System.Net.Mail.MailMessage
$MailMessage.From
='Administrator@Lab.Com'
$MailMessage.To.Add('kmliao@hotmail.com')
$MailMessage.Subject
= "Exchange 2010 Database Sizes"
$MailMessage.IsBodyHtml
=$True
$mailMessage.Body =$BodyText
$SmtpClient=New-Object
System.Net.Mail.Smtpclient
$Smtpclient.Host='SMTP.Telus.Net'
$SmtpClient.Send($MailMessage)
Run the script in EMS
Open Hotmail
