Get Exchange mailbox folder count using PowerShell

In this article we will learn how to get Exchange mailbox folder count using PowerShell commands. We will cover all the possible scenarios and used-case scenarios in this article.

Get Exchange mailbox folder count using PowerShell

This insightful article delves into the world of PowerShell commands tailored specifically for Exchange administrators. Learn how to wield the power of PowerShell to swiftly and accurately tally the number of folders within Exchange mailboxes.

Connect to Exchange Online PowerShell

To be able to run below PowerShell commands, you need to connect to Exchange Online PowerShell module. Open Windows PowerShell as administrator and run below commands one by one:

PowerShell
Install-Module ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline
Count number of folders in mailbox

In this example we will count number of folders in a mailbox using PowerShell. Replace “Concepts User” with the actual display name of the mailbox and run below PowerShell command:

PowerShell
# List names of folders in the specified mailbox
$folders = Get-MailboxFolderStatistics -Identity "Concepts User" | Select-Object -ExpandProperty FolderPath

# Count the number of folders
$folderCount = $folders.Count

# Display the count
Write-Host "Total number of folders in the mailbox: $folderCount"

The above command will display the count of folders and subfolders of “Concepts User” mailbox.

List folder name and get count of folders of a mailbox

In this example we will display the list of folders and subfolders of a mailbox, and then we will display the count of the folders. Replace “Concepts User” with the actual display name of the mailbox and run below PowerShell command:

PowerShell
# Get the list of folders in the specified mailbox
$folders = Get-MailboxFolderStatistics -Identity "Concepts User" | Select-Object -ExpandProperty FolderPath

# Display all the folders
Write-Host "Folders in the mailbox:"
$folders

# Count the number of folders
$folderCount = $folders.Count

# Display the count
Write-Host "Total number of folders in the mailbox: $folderCount"
Get count of folders of a mailbox excluding subfolders

In this example, we will list folders of a mailbox and display their count, but we will exclude the subfolders. Replace “Concepts User” with the actual display name of the mailbox and run below PowerShell command:

PowerShell
# Get the list of top-level folders in the specified mailbox
$folders = Get-MailboxFolderStatistics -Identity "Concepts user" | Where-Object { $_.FolderPath.Split('/').Count -eq 2 } | Select-Object -ExpandProperty FolderPath

# Display all the top-level folders
Write-Host "Top-level folders in the mailbox:"
$folders

# Count the number of top-level folders
$folderCount = $folders.Count

# Display the count
Write-Host "Total number of top-level folders in the mailbox: $folderCount"
Get count of subfolders of a mailbox

In this example we will list subfolders of a mailbox, then we will display the count of subfolders of a mailbox. Replace “Concepts User” with the display name of your mailbox and run below PowerShell script:

PowerShell
# Get the list of subfolders in the specified mailbox
$subfolders = Get-MailboxFolderStatistics -Identity "Concepts User" | Where-Object { $_.FolderPath.Split('/').Count -gt 2 } | Select-Object -ExpandProperty FolderPath

# Display all the subfolders
Write-Host "Subfolders in the mailbox:"
$subfolders

# Count the number of subfolders
$subfolderCount = $subfolders.Count

# Display the count
Write-Host "Total number of subfolders in the mailbox: $subfolderCount"
Get count of subfolders of Inbox folder of a mailbox

In this scenario we will list all the subfolders of Inbox folder of a mailbox and then we will display their count. Replace “[email protected]” with the actual email address of the mailbox and run below PowerShell script:

PowerShell
# Define the mailbox identity
$mailbox = "[email protected]"

# Get the list of subfolders in the Inbox folder of the specified mailbox
$subfolders = Get-MailboxFolderStatistics -Identity $mailbox -FolderScope Inbox | Where-Object { $_.FolderPath -notlike "*/Inbox/*" -and $_.FolderPath -ne "/Inbox" } | Select-Object -ExpandProperty FolderPath

# Display all the subfolders
Write-Host "Subfolders in the Inbox folder of mailbox $($mailbox):"
$subfolders

# Count the number of subfolders
$subfolderCount = $subfolders.Count

# Display the count
Write-Host "Total number of subfolders in the Inbox folder: $($subfolderCount)"

The above PowerShell script will display the list of subfolders of “Bob Ross” mailbox and will display their count as shown below:

Get count of subfolders of Inbox folder of a mailbox
List mailbox folders size and items count

In this scenario we will list mailbox folders size and items count for a mailbox. Replace “Concepts User” with the display name of the actual mailbox and run below PowerShell command:

PowerShell
Get-MailboxFolderStatistics -Identity "Concepts User" | Select-Object Name,FolderSize,ItemsInFolder
Get mailbox folder count from CSV

In this example we will use a CSV file to retrieve the count of mailbox folders.

Create CSV file with a column PrimarySmtpAddress that will include the email address of each mailbox for whom we want to get mailbox folder count as shown below:

Get mailbox folder count from CSV

Replace “C:\CSV Files\Mailboxfoldercount.csv” with the actual path of your CSV file and run below PowerShell script:

PowerShell
# Import the CSV file containing primary SMTP addresses
$mailboxList = Import-Csv -Path "C:\CSV Files\Mailboxfoldercount.csv"

# Loop through each mailbox in the CSV file
foreach ($mailbox in $mailboxList) {
    $primarySmtpAddress = $mailbox.PrimarySmtpAddress
    
    # Get the count of folders for the current mailbox
    try {
        $folderCount = (Get-MailboxFolderStatistics -Identity $primarySmtpAddress -FolderScope All).Count
        Write-Host ("Folder count for {0}: {1}" -f $primarySmtpAddress, $folderCount)
    } catch {
        Write-Host "Failed to retrieve folder count for $primarySmtpAddress"
    }
}

The above PowerShell script will fetch the mailboxes from CSV file and will display the folder count of each mailbox as shown below:

1 2
Get folder count of all mailboxes using PowerShell

In this example we will retrieve folder count of all the mailboxes in Exchange Online tenant.

PowerShell
# Get all mailboxes
$mailboxes = Get-Mailbox -ResultSize Unlimited

# Loop through each mailbox
foreach ($mailbox in $mailboxes) {
    $mailboxName = $mailbox.PrimarySmtpAddress
    
    # Get the count of folders for the current mailbox
    try {
        $folderCount = (Get-MailboxFolderStatistics -Identity $mailboxName -FolderScope All).Count
        Write-Host "Folder count for $($mailbox.PrimarySmtpAddress): $folderCount"
    } catch {
        Write-Host "Failed to retrieve folder count for $($mailbox.PrimarySmtpAddress)"
    }
}
Find count of empty folders of a mailbox

In this example we will find the empty folders and display the count of empty folders of a mailbox. Replace “Concepts User” with the actual display name of the mailbox, and run below PowerShell script:

PowerShell
# Define the mailbox identity
$mailbox = "Concepts User"

# Get all folders of the specified mailbox
$mailboxFolders = Get-MailboxFolderStatistics -Identity $mailbox

# Initialize a counter for empty folders
$emptyFolderCount = 0

# Initialize an array to store empty folders
$emptyFolders = @()

# Check each folder for emptiness
foreach ($folder in $mailboxFolders) {
    if ($folder.ItemsInFolder -eq 0) {
        $emptyFolderCount++
        $emptyFolders += $folder.FolderPath
    }
}

# Display all the empty folders
Write-Host "Empty folders in the mailbox:"
$emptyFolders

# Display the count of empty folders
Write-Host "Total number of empty folders in the mailbox: $emptyFolderCount"
Get count of folders created today in a mailbox

In this example we will find the folders those were created by the user today. We will list the folder names and the count of those folders. Replace “Concepts User” with the display name of the mailbox and run below PowerShell script:

PowerShell
# Define the mailbox identity
$mailbox = "Concepts User"

# Get the current date
$currentDate = Get-Date -Format "yyyy-MM-dd"

# Get all folders of the specified mailbox
$mailboxFolders = Get-MailboxFolderStatistics -Identity $mailbox

# Initialize a variable to count folders created today
$count = 0

# Display the name of folders created today
foreach ($folder in $mailboxFolders) {
    if ($folder.CreationTime.Date -eq $currentDate) {
        Write-Host "Folder: $($folder.FolderPath)"
        $count++
    }
}

# Display the total count of folders created today
Write-Host "Total number of folders created today: $count"

The above PowerShell script will display the name of folders created today in a mailbox and then it will display the count of the folders as shown below:

Get count of folders created today in a mailbox
Get count of folders created in last 10 days in a mailbox

In this example we will retrieve the folders created in last 10 days by a user in his mailbox and will display the count of folders. Replace “Concepts User” with the display name of the mailbox and run below PowerShell script:

PowerShell
# Define the mailbox identity
$mailbox = "Concepts User"

# Get the current date
$currentDate = Get-Date

# Calculate the date 10 days ago
$startDate = $currentDate.AddDays(-10)

# Get all folders of the specified mailbox
$mailboxFolders = Get-MailboxFolderStatistics -Identity $mailbox

# Initialize a variable to count folders created in the last 10 days
$count = 0

# Display the name of folders created in the last 10 days
foreach ($folder in $mailboxFolders) {
    if ($folder.CreationTime -ge $startDate) {
        Write-Host "Folder: $($folder.FolderPath)"
        $count++
    }
}

# Display the total count of folders created in the last 10 days
Write-Host "Total number of folders created in the last 10 days: $count"
Get count of folders created today in all mailboxes

In this scenario we will display the folders created by all users today, and then we will display the count of folders.

PowerShell
# Get all mailboxes
$mailboxes = Get-Mailbox -ResultSize Unlimited

# Get the current date
$currentDate = Get-Date -Format "yyyy-MM-dd"

# Initialize a variable to count folders created today
$count = 0

# Iterate through each mailbox
foreach ($mailbox in $mailboxes) {
    # Get all folders of the current mailbox
    $mailboxFolders = Get-MailboxFolderStatistics -Identity $mailbox.PrimarySmtpAddress
    
    # Iterate through each folder of the current mailbox
    foreach ($folder in $mailboxFolders) {
        # Check if the folder was created today
        if ($folder.CreationTime.Date -eq $currentDate) {
            # Display the folder path and the mailbox user
            Write-Host "Mailbox: $($mailbox.DisplayName), Folder: $($folder.FolderPath)"
            $count++
        }
    }
}

# Display the total count of folders created today
Write-Host "Total number of folders created today by all users: $count"
Get count of folders created in last 10 days in all mailboxes

In this scenario we will find folders of all mailboxes those were created by users in last 10 days, and then we will display the count of those folders.

PowerShell
# Get all mailboxes
$mailboxes = Get-Mailbox -ResultSize Unlimited

# Get the current date
$currentDate = Get-Date

# Calculate the date 10 days ago
$startDate = $currentDate.AddDays(-10)

# Initialize a variable to count folders created in the last 10 days
$count = 0

# Iterate through each mailbox
foreach ($mailbox in $mailboxes) {
    # Get all folders of the current mailbox
    $mailboxFolders = Get-MailboxFolderStatistics -Identity $mailbox.PrimarySmtpAddress
    
    # Iterate through each folder of the current mailbox
    foreach ($folder in $mailboxFolders) {
        # Check if the folder was created in the last 10 days
        if ($folder.CreationTime -ge $startDate) {
            # Display the folder path and the mailbox user
            Write-Host "Mailbox: $($mailbox.DisplayName), Folder: $($folder.FolderPath)"
            $count++
        }
    }
}

# Display the total count of folders created in the last 10 days
Write-Host "Total number of folders created in the last 10 days for all mailboxes: $count"
Get users with more than 50 folders in their mailbox

In this example we will find the users who have more than 50 folders (including subfolders) in their mailbox.

PowerShell
# Get all mailboxes
$mailboxes = Get-Mailbox -ResultSize Unlimited

# Initialize a variable to store users with more than 50 folders
$usersWithMoreThan50Folders = @()

# Iterate through each mailbox
foreach ($mailbox in $mailboxes) {
    # Get all folders of the current mailbox, including subfolders
    $folderCount = (Get-MailboxFolderStatistics -Identity $mailbox.PrimarySmtpAddress -IncludeAnalysis).Count
    
    # Check if the total folder count (including subfolders) is greater than 50
    if ($folderCount -gt 50) {
        # Add the user to the list of users with more than 50 folders
        $usersWithMoreThan50Folders += $mailbox.DisplayName
    }
}

# Display the users with more than 50 folders
Write-Host "Users with more than 50 folders including subfolders:"
$usersWithMoreThan50Folders
Get users with more than 100 subfolders in Inbox folder

In this example we will find the users who have more than 100 subfolders in their Inbox folder.

PowerShell
# Get all mailboxes
$mailboxes = Get-Mailbox -ResultSize Unlimited

# Initialize a variable to store users with more than 100 subfolders in Inbox
$usersWithMoreThan100Subfolders = @()

# Iterate through each mailbox
foreach ($mailbox in $mailboxes) {
    # Get the subfolders of the Inbox for the current mailbox
    $inboxSubfolders = Get-MailboxFolderStatistics -Identity $mailbox.PrimarySmtpAddress -FolderScope Inbox -IncludeAnalysis | 
                       Where-Object { $_.FolderPath -ne "Inbox" }

    # Count the number of subfolders in the Inbox
    $subfolderCount = $inboxSubfolders.Count
    
    # Check if the number of subfolders in the Inbox is greater than 100
    if ($subfolderCount -gt 100) {
        # Add the user to the list of users with more than 100 subfolders in Inbox
        $usersWithMoreThan100Subfolders += $mailbox.DisplayName
    }
}

# Display the users with more than 100 subfolders in Inbox
Write-Host "Users with more than 100 subfolders in Inbox:"
$usersWithMoreThan100Subfolders

Conclusion

In this article you learnt how to get Exchange mailbox folder count using PowerShell. You learnt how to count number of folders in mailbox, how to list mailbox folder size and items count, how to get mailbox folder count from CSV, and how to get mailbox folder count.

You might like our Other articles on Get mailbox folder size using PowerShell, Get Exchange Online mailbox size in GB, and How to get mailbox size report.

If you found this article helpful and informative, please share it within your community and do not forget to share your feedback in the comments below.

Join us on YouTube for the latest videos on the Cloud technology and join our Newsletter for early access of articles and updates.

Happy Scripting!!