Forum Discussion
lpmdvip
Feb 11, 2020Copper Contributor
Name, Department, #of Emails, Mailbox Size
Good afternoon!! Would anyone have a PS.1 script that would provide a list of all users, their department name, the number of emails in the mailbox, and the size of their mailbox?
lpmdvip
Feb 14, 2020Copper Contributor
Erick A. Moreno R. This worked perfectly!! Thank you!! One more thing - would you be able to add a column for the last time the mailbox was accessed?
Erick A. Moreno R.
Feb 14, 2020Iron Contributor
lpmdvip Here you have.
<#
.SYNOPSIS
Get MBXs Stats, User Attribute(s), Folder(s) Counts
.DESCRIPTION
.NOTES
Author: Erick A. Moreno
Email: emoreno@request-script.com
Date: 12 Jan 2020
PowerShell: v3
.References
#requested: https://techcommunity.microsoft.com/t5/windows-powershell/name-department-of-emails-mailbox-size/m-p/1167911
#>
$CredentialsFolder = "$home\Documents\"
$WorkingDir = "$home\Documents"
$ScriptTime = Get-Date -UFormat "%m%d%Y-%H%M"
#region Functions
Function _Get-Sessions
{
Param(
[Parameter(Mandatory=$true)]$CredentialsFolder,
[Parameter(Mandatory=$true)]$CredentialsFileName
)
#region Check for Credentials File
$TargetCredPath = $CredentialsFolder #Credential XML File Location
$TargetCredName = $TargetCredPath + $CredentialsFileName
#Write-Host $TargetCredName
If(Test-Path $TargetCredName)
{
$TargetCred = Import-Clixml $TargetCredName
}
Else
{
$Cred = Get-Credential
$Cred | Export-CliXml -Path $(Join-Path -path $TargetCredPath -ChildPath RemotePS.cred)
$TargetCred = Import-Clixml $TargetCredName
}
#endregion
#region connections
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $TargetCred -Authentication Basic -AllowRedirection
$NoOutput = Import-PSSession $Session -AllowClobber -Prefix O365 -CommandName "*Mailbox*","*User*"
#endregion
Return $Session
}
#endregion
#region Open PS Sessions
$TenantSession = _Get-Sessions -CredentialsFolder $CredentialsFolder -CredentialsFileName RemotePS.cred
#endregion
$MBXs = Get-O365Mailbox -ResultSize Unlimited
$Counter = $MBXs.count
$Results = @()
$NoDepartment = @()
$Counter
Foreach($MBX in $MBXs)
{
#Start-Sleep -Milliseconds 300
$PsSessionState = $(Get-PsSession $TenantSession.Id).State
If($($Counter/100) -match '^[0-9]+$' -or $PsSessionState -ne "Opened")
{
Remove-PSSession $TenantSession.Id -Confirm:$false
Write-Host "Opening a new Session Each 100 iterations" -ForegroundColor Yellow
$TenantSession = _Get-Sessions -CredentialsFolder $CredentialsFolder -CredentialsFileName RemotePS.cred
}
Write-Host "($Counter) Working On MBX: $($MBX.PrimarySmtpAddress)" -ForegroundColor Cyan
$Stats = $null
$UserData = $null
$FolderItems = $null
$UserData = Get-O365User $MBX.PrimarySmtpAddress
If(![string]::IsNullOrEmpty($($UserData.Department)))
{
$Stats = Get-O365MailboxStatistics -Identity $MBX.PrimarySmtpAddress
$FolderItems = Get-O365MailboxFolderStatistics -Identity $MBX.PrimarySmtpAddress | Where {$_.Name -match “Inbox|Sent Items|Deleted Items|Junk E-Mail”} | Select Name,ItemsInFolder #Add Folders as needed
$Properties = [Ordered]@{
DisplayName = $MBX.DisplayName
Department = $UserData.Department #Add properties as needed
UserPrincipalName = $MBX.UserPrincipalName
MailboxSizeMB = [math]::Round($($Stats.TotalItemSize.Value.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),4)
MailboxSizeGB = [math]::Round($($Stats.TotalItemSize.Value.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1GB),4)
InboxItems = $($FolderItems | where{$_.Name -eq "Inbox"}).ItemsInFolder
SentItems = $($FolderItems | where{$_.Name -eq "Sent Items"}).ItemsInFolder
DeletedItems = $($FolderItems | where{$_.Name -eq "Deleted Items"}).ItemsInFolder
JunkItems = $($FolderItems | where{$_.Name -eq "Junk E-Mail"}).ItemsInFolder
LastLogon = $Stats.LastLogonTime.ToString()
PrimarySMTPAddress = $MBX.PrimarySmtpAddress
}
$Results += New-Object PSObject -Property $Properties
}
Else
{
Write-Host "Department Field is Empty Skipping MBX: $($MBX.PrimarySmtpAddress)"
$NoDepartment += $($MBX.PrimarySmtpAddress)
}
$Counter--
}
$Results | Export-Csv -Path "$WorkingDir\ReportMBXsStats_$ScriptTime.csv" -NoTypeInformation -Encoding UTF8 -Force
$NoDepartment | Export-Csv -Path "$WorkingDir\NoDepartmentField_$ScriptTime.csv" -NoTypeInformation -Encoding UTF8 -Force
Write-Host "Report Saved To: $WorkingDir\ReportMBXsStats_$ScriptTime.csv" -ForegroundColor Cyan
Write-Host "Report Saved To: $WorkingDir\NoDepartmentField_$ScriptTime.csv" -ForegroundColor Cyan