Link to home
Start Free TrialLog in
Avatar of Michael Leonard
Michael LeonardFlag for United States of America

asked on

need assistance with a powershell script to export out mailbox statistics for a number of users

Can someone provide a way for me to run this script but add the "import-csv" option so that i can run it against multiple accounts? [100+]

here is the script that works perfectly for a single mailbox to export out folder statistics:
Get-MailboxFolderStatistics -id Joe_Smith | Select Identity,FolderSize,ItemsInFolder,NewestItemReceiveDate |Export-CSV c:\results.csv

thanks in advance!

Quest AD Cmdlet version would be fine as well

S.
Avatar of becraig
becraig
Flag of United States of America image

$accts = import-csv c:\accts.csv

$accts| foreach-object {do something}
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Assume u have user in each line u can run the following:

gc users.csv | %{
Get-MailboxFolderStatistics -id $_ | Select Identity,FolderSize,ItemsInFolder,NewestItemReceiveDate |Export-CSV c:\results.csv
}
$accts = import-csv c:\accts.csv

$accts| foreach-object {do something
#for specific values
Get-MailboxFolderStatistics -id $_.id | Select Identity,FolderSize,ItemsInFolder,NewestItemReceiveDate |Export-CSV c:\results.csv


}
$accts = import-csv c:\accts.csv
$outpath = c:\results.csv
$accts| foreach-object {do something
#for specific values
$mbinfo += Get-MailboxFolderStatistics -id $_.id | Select Identity,FolderSize,ItemsInFolder,NewestItemReceiveDate
}
$mbinfo | Select * | Export-Csv $outpath -NoTypeInformation
Export-Csv inside the foreach loop will cause the result file get overwritten for each user.. it should be placed outside the loop..
One approach would be to use custom objects to collect the information from each mailbox for later export. In this example, you'd have your list of users in a text file with a valid parameter (i.e. SamAccountName, DN, alias, etc.)
$folderStatsOutput = @()
Import-Csv c:\temp\users.txt | ForEach-Object {
   $folderStatsObj = Get-MailboxFolderStatistics -id $_
   $objTemp = New-Object PSObject -Property {
         Identity = $folderStatsObj.Identity
         FolderSize = "{0:0,0}"$folderStatsObj.FolderSize.ToKB()
         ItemsInFolder = $folderStatsObj.ItemsInFolder
         NewestItemReceiveDate = $folderStatsObj.NewestItemReceiveDate
    }
    $folderStatsOutput += $objTemp
}

$folderStatsOutput | 
Select-Object Identity,FolderSize,ItemsInFolder,NewestItemReceivedDate |
Export-Csv C:\temp\folderstats_results.csv -NoType

Open in new window

Sorry..
# change this line
Import-Csv c:\temp\users.txt | ForEach-Object {

# to this

Get-Content c:\temp\users.txt | ForEach-Object {

Open in new window

$results = @()
gc users.csv | %{
$results += , @(Get-MailboxFolderStatistics -id $_ | Select Identity,FolderSize,ItemsInFolder,NewestItemReceiveDate)
}

$results | export-csv c:\results.csv
Avatar of RTedrow
RTedrow

I can't test this at the moment but this was quick and dirty.  If the hash array does not work let me know and I'll rework it when I get to my desk:

$list=@()
foreach($user in cat c:\scripts\userlist.txt)
{
      $mbstat = Get-MailboxFolderStatistics -id $user Select

      $hash=@{
            Identity = $mbstat Identity
            Folder_Size = $mbstat FolderSize
            Items = $mbstat ItemsInFolder
            Newest_Item = $mbstat.NewestItemReceiveDate
            }

            $obj = New-Object PSObject -Property $hash

            $list += $obj
}

$list | export-csv C:\Scripts\MBInfo.csv
Avatar of Michael Leonard

ASKER

thanks all for the responses. Subsun you were first and this works perfectly. much appreciated.

S.