With PowerShell, how do I export all empty Exchange public folders, and permissions to those folders to csv?

Alex Nagy 20 Reputation points
2023-08-02T14:32:48.4833333+00:00

Using PowerShell, how can I extract all public folders that have an item count of zero, and include permission to those folders, to a csv file? I am cleaning up public folders, and want to start with removing empty ones, but I need to reach out to users that have permission to these folders.

Exchange | Exchange Server | Other
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Peter Kayode 506 Reputation points
    2023-08-02T14:43:17.73+00:00

    Maybe you wan to try this.

    # Get all public folders
    $publicFolders = Get-PublicFolder -Recurse
    
    # Create an empty array to hold the results
    $results = @()
    
    # Iterate over each public folder
    foreach ($folder in $publicFolders) {
        # Check the item count
        $stats = Get-PublicFolderItemStatistics $folder.Identity
        if ($stats.Count -eq 0) {
            # If the item count is zero, get the permissions
            $permissions = Get-PublicFolderClientPermission $folder.Identity
            foreach ($permission in $permissions) {
                # For each permission, create a custom object with the details we want
                $result = New-Object PSObject -Property @{
                    "FolderName" = $folder.Identity
                    "User" = $permission.User
                    "AccessRights" = $permission.AccessRights
                }
                # Add the result to our results array
                $results += $result
            }
        }
    }
    
    # Export the results to a CSV file
    $results | Export-Csv -Path "EmptyPublicFolders.csv" -NoTypeInformation
    	
    

    This script will create a CSV file called EmptyPublicFolders.csv with columns for FolderName, User, and AccessRights. Each row will represent a permission for a user on a public folder with zero items. You can then use this CSV to contact the users.


1 additional answer

Sort by: Most helpful
  1. Alex Nagy 20 Reputation points
    2023-08-02T17:22:14.97+00:00

    Thanks, Peter. This greatly helped me. The only change I added was the following:

    $publicFolders = Get-PublicFolder -Recurse | ? {$_.HasSubFolders -eq $false}

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.