John Yassa's Blog

Home » Exchange 2003 » Get Mailboxes size for users in a specified OU (VB Script)

Get Mailboxes size for users in a specified OU (VB Script)

‘this is a VB script that got the OU variable and an array of Back end servers , and iterates every server and match the users with the users in the OU

‘Option Explicit

‘On Error Resume Next

Dim ServerList                  ‘ List of computers to check

Dim server                                          ‘ Current computer to check

Dim fso                                                                ‘ File System Object

Dim strWinMgmts                                           ‘ Connection string for WMI

Dim objWMIExchange                   ‘ Exchange Namespace WMI object

Dim listExchange_Mailboxs        ‘ ExchangeLogons collection

Dim objExchange_Mailbox                          ‘ A single ExchangeLogon WMI object

Dim logfile                                         ‘ Output file

Dim ou1

Dim provider

Dim objParent

Dim WshShell

Dim objUser

‘Set objParent = GetObject(provider & ou1)

ou1 = “OU LDAP Path”

provider = “LDAP://”

Const cWMINameSpace = “root/MicrosoftExchangeV2”

Const cWMIInstance = “Exchange_Mailbox”

Const LOG_FILE = “EMailSize.csv”

set objParent = GetObject(provider & ou1)

objParent.Filter = Array(“user”)


‘ Set up the email servers


ServerList  = Array(“Mailbox server 1″,”Mailbox server 2”)


‘ Set up log file


set fso = CreateObject(“Scripting.FileSystemObject”)

Set logfile = fso.CreateTextFile(LOG_FILE)

logfile.WriteLine(“””Display Name””,””Mailbox Size””,””Mailbox TotalItems””,””Mailbox StoreName””,””Mailbox ServerName””,””Mailbox LegacyDN”””)

‘ Create the object string, indicating WMI (winmgmts), using the
‘ current user credentials (impersonationLevel=impersonate),
‘ on the computer specified in the constant cComputerName, and
‘ using the CIM namespace for the Exchange provider.

WScript.Echo “Starting now”

‘The rest of the script will fetch mailbox sizes for our servers. Mailbox sizes are in Kilobytes.

For Each server in ServerList

WScript.Echo “Starting ” & server & ” search.”

strWinMgmts = “winmgmts:{impersonationLevel=impersonate}!//” & server & “/” & cWMINameSpace

‘WScript.Echo strWinMgmts

Set objWMIExchange =  GetObject(strWinMgmts)

‘ Verify we were able to correctly set the object.

If Err.Number <> 0 Then

WScript.Echo “ERROR: Unable to connect to the WMI namespace.”


‘The Resources that currently exist appear as a list of

‘Exchange_Mailbox instances in the Exchange namespace.

Set listExchange_Mailboxs = objWMIExchange.InstancesOf(cWMIInstance)

‘ Were any Exchange_Mailbox Instances returned?

If (listExchange_Mailboxs.count > 0) Then

‘ If yes, do the following:
‘ Iterate through the list of Exchange_Mailbox objects.

For each objUser in objParent

For Each objExchange_Mailbox in listExchange_Mailboxs
‘logfile.writeline(“””legacyExchangeDN””,”& objUser.legacyExchangeDN &””)’ ‘logfile.writeline(“””legacyDN””,”& objExchange_Mailbox.LegacyDN &””)

If  ucase(objUser.legacyExchangeDN) = ucase(objExchange_Mailbox.LegacyDN) then

‘ Display the value of the Size property.
wscript.echo “Matched”
wscript.echo objUser.legacyExchangeDN

logfile.WriteLine(“””” & objExchange_Mailbox.MailboxDisplayName & “””,””” & objExchange_Mailbox.Size & “””,””” & objExchange_Mailbox.TotalItems & “””,””” & objExchange_Mailbox.StoreName & “””,””” & objExchange_Mailbox.ServerName & “””,””” & objExchange_Mailbox.LegacyDN & “”””)






‘ If no Exchange_Mailbox instances were returned, display that.

WScript.Echo “WARNING: No Exchange_Mailbox instances were returned.”

End If

End If


Wscript.Echo “Completed”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: