################################# # Count emails in PST file # # MK 11/2016 # # Rev1.0 # ################################# #Check if Outlook is installed Get-ItemProperty HKLM:\SOFTWARE\Classes\Outlook.Application | Select-Object PSPath -OutVariable outlook if (!$outlook -match "Outlook.Application"){ Write-Host "Outlook is not installed on this machine, Press any key to continue ..." $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown'); } #Path to PST File $strPSTPath = "PATHTOPST" #Create Outlook COM Object $objOutlook = New-Object -com Outlook.Application $objNameSpace = $objOutlook.GetNamespace("MAPI") #Try to load the PST into Outlook try { $objNameSpace.AddStore($strPSTPath) } catch { Write-Host "Could not load pst - usually this is because the file is locked by another process or is too big (try opening in outlook to see the full error) Press any key to continue ..." $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown'); } #Try to load the Outlook Folders try { $PST = $objnamespace.stores | ? { $_.FilePath -eq $strPSTPath } } catch { Write-Host "You have another PST added to outlook that cannot be accessed or found, please remove then re-run this script. Press any key to continue ..." $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown'); } #Browse to PST Root $root = $PST.GetRootFolder() #Get top level folders $subfolders = $root.Folders #count items in PST Root $rootcount = $root.items.count #Write root count (usually zero) Write-Host "PST Root Folder Contains" $rootcount "Items" #Start Counter $counter = $rootcount #Iterate all folders (sub folders, ONLY 6 layers deep) # root ("/")- L1 -L2 -L3-L4-L5-L6 # L1 foreach ($L1folder in $subfolders) { $count = $L1folder.items.count Write-Host "Folder" $L1folder.FolderPath "Contains" $count "Items" $counter += $count # L2 foreach ($L2folder in $L1folder.Folders) { $count = $L2folder.items.count Write-Host "Folder" $L2folder.FolderPath "Contains" $count "Items" $counter += $count # L3 foreach ($L3folder in $L2folder.Folders) { $count = $L3folder.items.count Write-Host "Folder" $L3folder.FolderPath "Contains" $count "Items" $counter += $count # L4 foreach ($L4folder in $L3folder.Folders) { $count = $L4folder.items.count Write-Host "Folder" $L4folder.FolderPath "Contains" $count "Items" $counter += $count # L5 foreach ($L5folder in $L4folder.Folders) { $count = $L5folder.items.count Write-Host "Folder" $L5folder.FolderPath "Contains" $count "Items" $counter += $count # L6 foreach ($L6folder in $L5folder.Folders) { $count = $L6folder.items.count Write-Host "Folder" $L6folder.FolderPath "Contains" $count "Items" $counter += $count # L7 foreach ($L7folder in $L6folder.Folders) { $count = $L7folder.items.count Write-Host "Folder" $L7folder.FolderPath "Contains" $count "Items" $counter += $count # L8 foreach ($L8folder in $L7folder.Folders) { $count = $L8folder.items.count Write-Host "Folder" $L8folder.FolderPath "Contains" $count "Items" $counter += $count } } } } } } } } #Write total count Write-Host "Total Items" $counter -ForegroundColor Red