SOLVED

Does someone have a script already written that outputs 365 mailbox sizes and archive sizes in GB as

Iron Contributor

Hello,

If anyone has the time to help I would appreciate your help for something that is beyond my skill set.   I spent hours on this with no luck

I can output mailbox sizes in GB with this script

Get-Mailbox -ResultSize Unlimited  | Get-MailboxStatistics |
Select-Object -Property @{label=”User”;expression={$_.DisplayName}},
@{label=”Total Size (GB)”;expression={[math]::Round(`
($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1GB),2)}}

 

I can output archive mailbox sizes with this script

Get-Mailbox -resultsize unlimited | Select-Object name, @{n="Archive Size";e={(Get-MailboxStatistics -archive $_.identity).TotalItemSize}},@{label="(GB)";expression={[math]::Round(`
($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1GB),2)}} | ft

 

I would like a single csv output with both the archive and mailbox sizes next to the person's name and in GB as the measurement.

This script will output both however it won't put them in GB. 

connect-msolservice
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking  
Set-ExecutionPolicy RemoteSigned
Enable-PSRemoting


Write-Host "Gathering Stats, Please Wait.." 
  
$Mailboxes = Get-Mailbox -ResultSize Unlimited | Select UserPrincipalName, identity, ArchiveStatus 
  
$MailboxSizes = @() 
  
foreach ($Mailbox in $Mailboxes) { 
  
                $ObjProperties = New-Object PSObject 
                
                $MailboxStats = Get-MailboxStatistics $Mailbox.UserPrincipalname | Select LastLogonTime, TotalItemSize, ItemCount 
                
                Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "UserPrincipalName" -Value $Mailbox.UserPrincipalName 
                Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Last Logged In" -Value $MailboxStats.LastLogonTime 
                Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox Size" -Value $MailboxStats.TotalItemSize 
                Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox Item Count" -Value $MailboxStats.ItemCount 
                
                if ($Mailbox.ArchiveStatus -eq "Active") { 
                
                                $ArchiveStats = Get-MailboxStatistics $Mailbox.UserPrincipalname -Archive | Select TotalItemSize, ItemCount 
                                
                                Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size" -Value $ArchiveStats.TotalItemSize 
                                Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Item Count" -Value $ArchiveStats.ItemCount 
  
                } 
                else { 
                
                                Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size" -Value "No Archive" 
                                Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Item Count" -Value "No Archive" 
                                
                } 
                
                $MailboxSizes += $ObjProperties 
  
}              
                
$MailboxSizes | Export-csv "C:\inboxsizes.csv" 
  
Get-PSSession | Remove-PSSession

 

I'm asking for someone who has a script or a fix for my script to assist please with the specific change to where in the script great!   If your solution is to learn scripting I appreciate the advice but it is unlikely I can come up with the solution.  

I tried the IT scripting guys forum and that was the response I got.

 

 

 

 

8 Replies

All you need to do is add the "calculation" to the output, as in replace it with something like this:

 

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size" -Value 

[math]::Round(($ArchiveStats.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1GB),2)

@Vasil Michev 

Thank you so much for your response!

 

I should be clear - I am OK with about a 1 line script.   Anything beyond that is beyond my skill level.   I tried to put that in but had no luck.   I do appreciate any help but I regret  to say I'll need the answer spoon fed as to which script and exactly where to make the changes.   I tested with the 3 scripts I posted and couldn't get it to work.  I'm not saying your answer isn't helpful - just that I couldn't figure out how to make it work.    Thanks again - sorry but powershell eludes me.

Just replace the corresponding line in the "long" script with the one I pasted above - if this generates the output you expect, you can do the same for the other "size" field.

@Vasil Michev 

 

Thanks.

 

I tried this - 


Write-Host "Gathering Stats, Please Wait.."

$Mailboxes = Get-Mailbox -ResultSize Unlimited | Select UserPrincipalName, identity, ArchiveStatus

$MailboxSizes = @()

foreach ($Mailbox in $Mailboxes) {

$ObjProperties = New-Object PSObject

$MailboxStats = Get-MailboxStatistics $Mailbox.UserPrincipalname | Select LastLogonTime, TotalItemSize, ItemCount

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "UserPrincipalName" -Value $Mailbox.UserPrincipalName
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Last Logged In" -Value $MailboxStats.LastLogonTime
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox Size" -Value $MailboxStats.TotalItemSize
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox Item Count" -Value $MailboxStats.ItemCount

if ($Mailbox.ArchiveStatus -eq "Active") {

$ArchiveStats = Get-MailboxStatistics $Mailbox.UserPrincipalname -Archive | Select TotalItemSize, ItemCount

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size" -Value

[math]::Round(($ArchiveStats.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1GB),2)
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Item Count" -Value $ArchiveStats.ItemCount

}
else {

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size" -Value "No Archive"
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Item Count" -Value "No Archive"

}

$MailboxSizes += $ObjProperties

}

$MailboxSizes | Export-csv "C:\inboxsizes.csv"

 

 

I'm getting 

Add-Member : Missing an argument for parameter 'Value'. Specify a parameter of type 'System.Object' and try again.
At line:16 char:118
+ ... t $ObjProperties -MemberType NoteProperty -Name "Archive Size" -Value
+ ~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Add-Member], ParameterBindingException
+ FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.AddMemberCommand

best response confirmed by Boe Dillard (Iron Contributor)
Solution

You seem to have inserted a new line there. Try this:

 

Write-Host "Gathering Stats, Please Wait.."

$Mailboxes = Get-Mailbox -ResultSize Unlimited | Select UserPrincipalName, identity, ArchiveStatus

$MailboxSizes = @()

foreach ($Mailbox in $Mailboxes) {

$ObjProperties = New-Object PSObject

$MailboxStats = Get-MailboxStatistics $Mailbox.UserPrincipalname | Select LastLogonTime, TotalItemSize, ItemCount

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "UserPrincipalName" -Value $Mailbox.UserPrincipalName
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Last Logged In" -Value $MailboxStats.LastLogonTime
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox Size" -Value ([math]::Round(($MailboxStats.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1GB),2))
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox Item Count" -Value $MailboxStats.ItemCount

if ($Mailbox.ArchiveStatus -eq "Active") {

$ArchiveStats = Get-MailboxStatistics $Mailbox.UserPrincipalname -Archive | Select TotalItemSize, ItemCount

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size" -Value ([math]::Round(($ArchiveStats.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1GB),2))
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Item Count" -Value $ArchiveStats.ItemCount

}
else {

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size" -Value "No Archive"
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Item Count" -Value "No Archive"

}

$MailboxSizes += $ObjProperties

}

$MailboxSizes | Export-csv "C:\inboxsizes.csv" -NoTypeInformation

@Vasil Michev 

 

AMAZING work!!    Thanks so very much!!  I've been kicking that around for over a day with zero progress and you solved it just like that.   

 

This is where I'm at with the code now and if you don't mind - how do I add the user name in addition to their login?   If you don't have time - I completely understand you have been wonderful!

 

 

Write-Host "Gathering Stats, Please Wait.."

$Mailboxes = Get-Mailbox -ResultSize Unlimited | Select UserPrincipalName, identity, ArchiveStatus

$MailboxSizes = @()

foreach ($Mailbox in $Mailboxes) {

$ObjProperties = New-Object PSObject

$MailboxStats = Get-MailboxStatistics $Mailbox.UserPrincipalname | Select LastLogonTime, TotalItemSize, ItemCount

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "UserPrincipalName" -Value $Mailbox.UserPrincipalName
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Last Logged In" -Value $MailboxStats.LastLogonTime
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox Size (GB)" -Value ([math]::Round(($MailboxStats.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1GB),2))


if ($Mailbox.ArchiveStatus -eq "Active") {

$ArchiveStats = Get-MailboxStatistics $Mailbox.UserPrincipalname -Archive | Select TotalItemSize, ItemCount

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size (GB)" -Value ([math]::Round(($ArchiveStats.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1GB),2))
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Item Count" -Value $ArchiveStats.ItemCount

}
else {

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size (GB)" -Value "0"


}

$MailboxSizes += $ObjProperties

}

$MailboxSizes | Export-csv "C:\inboxsizes.csv" -NoTypeInformation

 

Thank you for your wonderful help!

Insert another line in between the Add-Member ones

 

 

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "DisplayName" -Value $Mailbox.DisplayName

 

 

@Vasil Michev   Thanks again!!!!

 

For anyone wanting to know how it looks

 

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking
Set-ExecutionPolicy RemoteSigned
Enable-PSRemoting


Write-Host "Gathering Stats, Please Wait.."
$Mailboxes = Get-Mailbox -ResultSize Unlimited | Select UserPrincipalName, identity, ArchiveStatus
$MailboxSizes = @()
foreach ($Mailbox in $Mailboxes) {
$ObjProperties = New-Object PSObject
$MailboxStats = Get-MailboxStatistics $Mailbox.UserPrincipalname | Select DisplayName, LastLogonTime, TotalItemSize, ItemCount
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Display Name" -Value $MailboxStats.DisplayName
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "UserPrincipalName" -Value $Mailbox.UserPrincipalName
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Last Logged In" -Value $MailboxStats.LastLogonTime
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox Size (GB)" -Value ([math]::Round(($MailboxStats.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1GB),2))

if ($Mailbox.ArchiveStatus -eq "Active") {
$ArchiveStats = Get-MailboxStatistics $Mailbox.UserPrincipalname -Archive | Select TotalItemSize, ItemCount
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size (GB)" -Value ([math]::Round(($ArchiveStats.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1GB),2))
}
else {
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size (GB)" -Value "0"

}
$MailboxSizes += $ObjProperties
}
$MailboxSizes | Export-csv "C:\inboxsizes.csv" -NoTypeInformation

Get-PSSession | Remove-PSSession

1 best response

Accepted Solutions
best response confirmed by Boe Dillard (Iron Contributor)
Solution

You seem to have inserted a new line there. Try this:

 

Write-Host "Gathering Stats, Please Wait.."

$Mailboxes = Get-Mailbox -ResultSize Unlimited | Select UserPrincipalName, identity, ArchiveStatus

$MailboxSizes = @()

foreach ($Mailbox in $Mailboxes) {

$ObjProperties = New-Object PSObject

$MailboxStats = Get-MailboxStatistics $Mailbox.UserPrincipalname | Select LastLogonTime, TotalItemSize, ItemCount

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "UserPrincipalName" -Value $Mailbox.UserPrincipalName
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Last Logged In" -Value $MailboxStats.LastLogonTime
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox Size" -Value ([math]::Round(($MailboxStats.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1GB),2))
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox Item Count" -Value $MailboxStats.ItemCount

if ($Mailbox.ArchiveStatus -eq "Active") {

$ArchiveStats = Get-MailboxStatistics $Mailbox.UserPrincipalname -Archive | Select TotalItemSize, ItemCount

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size" -Value ([math]::Round(($ArchiveStats.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1GB),2))
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Item Count" -Value $ArchiveStats.ItemCount

}
else {

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size" -Value "No Archive"
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Item Count" -Value "No Archive"

}

$MailboxSizes += $ObjProperties

}

$MailboxSizes | Export-csv "C:\inboxsizes.csv" -NoTypeInformation

View solution in original post