Today’s script is an extension of yesterday’s script.
What the script does:
- get all exchange servers from your AD domain
- make a wmi connection to each server and create a list of the mailboxes and their size
The script is tested in an win2003/exchange2003 environment.
Follow the next steps to run the script (admin rights needed):
- copy and paste the script in your favorite text editor
- save the script (for example c:\temp\listallmailboxes.vbs)
- open a command prompt
- go to “c:\temp”
- give “cscript listallmailboxes.vbs” (without quotes) and enter
The script:
' Name : listallmailboxes.vbs
' Description : script to enumerate all mailboxes and their size in your AD domain
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 07-04-2010
' Level: intermediate
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
Set objRootDSE = GetObject("LDAP://RootDSE")
strBase = "<LDAP://" & objRootDSE.Get("configurationnamingcontext") & ">"
strFilter = "(objectCategory=msExchExchangeServer)"
strAttributes = "name"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute
Do Until adoRecordset.EOF
Set objWMIExchange = GetObject("winmgmts:{impersonationLevel=impersonate}!//"&_
adoRecordset.Fields("name").Value & "/root/MicrosoftExchangeV2")
Set colExchangeMailboxes = objWMIExchange.InstancesOf("Exchange_Mailbox")
For Each objExchangeMailbox in colExchangeMailboxes
If Left(objExchangeMailbox.StorageGroupName, 5) <> "Recov" Then
Wscript.Echo adoRecordset.Fields("name").Value & " ; " & objExchangeMailbox.MailboxDisplayName & " ; " &_
Round(objExchangeMailbox.Size/1024,0) & " MB"
End If
Next
Set colExchange_Mailboxes = Nothing
Set objWMIExchange = Nothing
adoRecordset.MoveNext
Loop
adoRecordset.Close
adoConnection.Close
Set adoRecordset = Nothing
Set objRootDSE = Nothing
Set adoConnection = Nothing
Set adoCommand = Nothing
When you have problems/questions please post a reply or give a ‘star’ rating.
Happy scripting.
Best regards,
Dirk Adamsky – Deludi BV

Dirk,
Thanks for your script.
I would like to use your script for a specific exchange server and a specific OU.
Hoe can i parameter it?
Thanks in advance,
Kind regards,
Verdure
Hi Verdure,
You can do that by changing the ADO filter line.
For example when the servername is srv001 =>
strFilter = “(&(objectCategory=msExchExchangeServer)(cn=srv001))”
For the OU: you can change the value of strBase.
For example:
Best regards,
dirk adamsky
Hi Dirk,
It is a very efficient script,
I was wondering if there is a way to add in an input for a user ID or Email address on a Domain
To get the result for the one user only
Thank you
JAMAL
Hi Jamal,
This particular script is not very practical to get the mailbox size of a single user.
I will make a new script for you next monday.
Best regards,
Dirk Adamsky
Thank you very much Dirk,
I really appreciate your help please let me when you have it ready
Many Thanks
JAMAL
Hi Jamal,
I have made an extension to the script.
It can be found here:
http://deludi.nl/blog/vbscript/active-directory/exchange-active-directory-vbscript/active-directory-and-wmi-vbscript-to-show-the-size-of-a-specific-mailbox/
Please let me know if it works for you.
Best regards,
Dirk Adamsky
hi Dirk,
it works perfect; only for one issue you need to run it under an Admin account if not you get the error below:
“permission denied ‘GetObject’ ”
is there a way to avoid this error by allowing everyone to use the script within the domain
Another request;
would be available to add more output data size as :
Outbox
Sent Items
Deleted Items
Calendar
Many Thanks
JAMAL
Nice, perfect. Two thumbs up in a circle.
Hello! I need the script to running on exchange 2003 , server 2003 and provide users names and office information (attributes of AD) for each user….can that be done? Thank you in advance!
Hi Duwane,
I will extend the script for you next monday.
Best regards,
Dirk Adamsky
Thanks! I have be able to query all mailboxes on all servers and provided sizes and be able to report if they are over their limit and have much by and user information (office, billing code, organization)….THANKS!
Thanks again on editing the script…just let me know when
Is it possible to extend the script to obtain the exchange version (2000/2003/200… and profession/ standard)?
Thanks in advance,
Hi Copper,
It is certainly possible but in general you, as an administrator, know what Exchange version is used.
I have written the script for a Win 2003 and Exchange 2003 environment. It is not tested for 2000 or 2007 environments.
Best regards,
Dirk Adamsky
Hi Dirk,
Could you change your “VBscript”to enumerate all mailNickname – DispalyName – mailboxes and their size in your AD domain
Thanks in advance,
Verdural
Hi Dirk,
i tried to lock this script to one OU, but when i fill in the ldap Path of that OU the script stops with the error that the AD Path ist wrong. can you give me an example for the correct syntax?
i tried
strBase = “” & objRootDSE.Get(“configurationnamingcontext”) & “>”
this path is correct but not found.
regards and thanks for your good work
Hi Corn,
Sorry for my late reply.
To run the script on a specific OU please replace these 2 lines:
Set objRootDSE = GetObject("LDAP://RootDSE") strBase = "<LDAP://" & objRootDSE.Get("configurationnamingcontext") & ">"with
Set objRootDSE = GetObject(“LDAP://ou=mailserver,dc=test,dc=org”)
Best regards,
Dirk Adamsky
Hi Dirk,
I would really like to use your script and I made the mod to only point to one server, but I am not getting anything. I also moded it to write the output to a file.
Thanks for the script, can you help with my problem?
Hi,
Sorry i dont get it
i replaced the lines as you said. but when i try to run the script, it comes to an error an line 24,1 (set adoRecordset)
regards
corn
Hi corn,
sorry for my late reply.
Can you please post the contents of your line that starts with ‘Set objRootDSE =’.
Best regards,
Dirk Adamsky
Hi Jeff Doty,
Sorry for my late reply.
I decided to make a script for you.
It can be found here:
http://deludi.nl/blog/vbscript/active-directory/exchange-active-directory-vbscript/vbscript-and-wmi-vbscript-to-enumerate-all-mailboxes-on-a-given-exchange-server/
Hope this is what you want.
Best regards,
Dirk Adamsky
Hi, thanks for the script, can the output be sorted by mailbox size?
Hi Gavin,
that can be done but will require some extra coding.
I think the is a good option.
Easier though is to output to a .txt or .csv file and import that file in Excel.
You can use the Excel sorting functions.
Here’s the steps to create the file:
1. open a command prompt
2. go to the script directory
3. give “cscript listallmailboxes.vbs > listallmailboxes.txt” (without the quotes)
4. start Excel and open the listallmailboxes.txt file
5. select the ; as separator
Best regards,
Dirk Adamsky
Hi Dirk,
Thanks for the suggestion but I have used the script to auto email to me as my Mailbox store is always hitting it’s max of 75 Gb, it would be nice if I didn’t have to manually edit the data each day.
Thanks
Hi Gavin,
I will write a script for you.
Please give me a couple of days.
Best regards,
dirk adamsky
Many thanks Dirk
Hi Gavin,
I did some research: vbscript does not have methods for sorting arrays.
I have to use a bubble sort function to do the sorting.
This is a somewhat slow and cpu intensive option.
There is an escape: when you have a .net runtime on the machine where the script runs you can use an Arraylist (System.Collections.Arraylist).
The Arraylist object has a sort funtion.
Now the golden question: do you have .net on your script server or are you able to install it/
Best regards,
dirk adamsky
Hi Dirk,
Sorry for delay, been on vacation. Yes we do have .NET on the server.
Thanks
Gavin
Hi Gavin,
I have a new script for you.
It can be found here:
http://deludi.nl/blog/vbscript/active-directory/email/active-directory-and-wmi-vbscript-to-enumerate-a-sorted-list-of-all-mailboxes-and-their-size-in-your-ad-domain/
Best regards,
dirk adamsky
Dirk,
Boy, you sure are doing a lot of work for folks. You are a nice guy on top of being a hell of a scripter. I was wondering how I might use your script to dump the output to a .CSV so that it can be analyzed in excel. Any help would be greatly appreciated. I really like the simple output of your original script but it’s coming up in individual message boxes 1 by 1 and I have to click ok over and over (or kill WSH all together). Just want the output dumped to a file and don’t know how to go about it.
Thanks!!!
Tom
Dirk..
I’m an idiot. Don’t waste your time responding. I just executed at the command line and piped the output into a file. Management has made me rusty. My apologies for cluttering your blog.
Tom
Hi Tom,
No problem.
There are no dumb questions.
Best regards,
dirk adamsky
I’m not sure what I’m doing wrong. I hope you can help.
I’m running command prompt as administrator and when I type:
C:\temp>cscript.exe templistallmailboxes.vbs
I get the following:
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
C:\temp\templistallmailboxes.vbs(28, 2) Microsoft VBScript runtime error: Permission denied: ‘GetObject’
What am I doing wrong?
Hope you can Help,
Mohamed Imran
Okay, I figured it out – the script works fine when run directly from the server.
Thank you.
Mohamed Imran
Hi Mohamed,
good to hear that you solved it.
Happy scripting.
Best regards,
Dirk Adamsky
Thanks for this script Dirk, it does exactly what i need!
This is amazing, but is there a way we could include displaying the users outlook details as well, e.g. company?
Hi Lefty,
I already created a script to enumerate the outlook details.
It can be found here:
Best regards,
Dirk Adamsky
Sorry forgot th link:
http://vbscriptblog.com/vbscript/active-directory/outlook/active-directory-vbscript-to-enumerate-the-outlook-details-of-all-users-in-your-company/
Best regards,
Dirk Adamsky
Hi Dirk,
I am trying to use this script and unfortunately am getting errors. I am running it from a 2003 exchange server. When I run the script exactly as it is here I get an error:
“Microsoft VBScript runtime error: The remote server machine does not exist or is unavailable: ‘GetObject’”
When I try and run it just on one OU (which is really what I need to do) I get the error:
“Provider: Table does not exist”
Here are the two lines I saw you requested from a poster earlier:
Set objRootDSE = GetObject(“LDAP://RootDSE”)
strBase = “”
Not sure why it didnt show up… the strBase line I am using is:
strBase = “”
Hi Chris,
Sorry for my late reply (was very busy with other projects).
The line for strBase should be:
strBase = "<LDAP://" & objRootDSE.Get("configurationnamingcontext") & ">"
This stands for your local domain.
I had to use the WordPress plugin for publishing sourcecode…
In the regular comment field the line is shown like strBase = “”.
Everything between the quotes diappears…..
Happy scripting.
Best regards,
Dirk Adamsky