1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
## https://gallery.technet.microsoft.com/office/Delete-Outlook-Folders-f092eb15/ # Script: DeleteFoldersBottomUp.ps1 # Purpose: This scripts deletes every folder and subfolders for a specific top-folder starting from the last one # Author: Twan van Beers (based on original script from Nuno Mota) # Date: May2015 [String] $mbxName = “someone@somewhere.com” [String] $topFolderName = “Deleted Items” [String] $dllPath = “C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll” [Void] [Reflection.Assembly]::LoadFile($dllPath) $Service = New–Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1) $Service.AutodiscoverUrl($mbxName, {$True}) $RootFolderID = new–object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root, $mbxName) $RootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service, $RootFolderID) $FolderView = New–Object Microsoft.Exchange.WebServices.Data.FolderView(1000) $FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep # set up the search filter to look for the folder we want $SearchFilter = New–Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo( [Microsoft.Exchange.WebServices.Data.FolderSchema]::Displayname, $topFolderName ) $Response = $RootFolder.FindFolders($SearchFilter, $FolderView) if( $Response.folders.count –eq 1) { $AllSubFolders = @() # Found the folder, so start a new Deep search across all its sub-folders $FolderView = New–Object Microsoft.Exchange.WebServices.Data.FolderView(1000) $FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep $RootFolder = $Response.folders[0] # get all of the subfolders by incrementing the Offset until nothing more to get $MoreResults = $true do{ $Response = $RootFolder.FindFolders($FolderView) $AllSubFolders += $Response.Folders $MoreResults = $Response.MoreAvailable $FolderView.Offset += $Response.Folders.count } while( $MoreResults –eq $true ) # now we have all of the folders let’s delete them from the bottom up for( $i = ($AllSubFolders.Count – 1); $i –gt 0; $i— ) { $i $error.clear() $AllSubFolders[$i].Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete) # sometimes it seems to time out (I had over 8000 folders to delete) if so then just retry the same one next time if( $error[0] –like ‘*the operation has timed out*’ ) { $i++ } } } |
1 2 3 4 5 6 7 |
total_messages = oFldr.Items.Count For i = 1 To total_messages message_index = total_messages – i + 1 Set oMessage = oFldr.Items.Item(message_index) oMessage.Delete Set oMessage = Nothing Next |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
Public Sub DeletindEmtpyFolder() Dim mytoplvl Dim delFlag As Boolean Set mytoplvl = Outlook.GetNamespace(“MAPI”).PickFolder.Folders Do FolderPurge mytoplvl, delFlag Loop Until delFlag = False Debug.Print ” Done.” End Sub Public Sub FolderPurge(mytoplvl, delFlag) Dim myFldr As Folder ‘Declare sub folder objects delFlag = False If mytoplvl.Count <> 0 Then Debug.Print “Analyzing: “ & mytoplvl.GetFirst.Name & ” delFlag:” & delFlag For Each myFldr In mytoplvl ‘Sweep through each folder under the inbox If myFldr.Items.Count < 1 Then ‘If the folder is empty check for subfolders If myFldr.Folders.Count < 1 Then ‘If the folder contains not sub folders confirm deletion Debug.Print myFldr.Name & ” contains no items and no subfolders, and will be deleted.” myFldr.Delete ‘Delete the folder delFlag = True Else ‘Folder contains sub folders so confirm deletion FolderPurge myFldr.Folders, delFlag End If Else ‘Folder contains items or (subfolders that may be empty). FolderPurge myFldr.Folders, delFlag End If Next myFldr Else Debug.Print “The folder does not contain any sub folders” & ” delFlag:” & delFlag End If End Sub |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
Public Sub DeletindEmtpyFolder() Dim mytoplvl Dim delFlag As Boolean Set mytoplvl = Outlook.GetNamespace(“MAPI”).PickFolder.Folders Do FolderPurge mytoplvl, delFlag Loop Until delFlag = False Debug.Print ” Done.” End Sub Public Sub FolderPurge(mytoplvl, delFlag) Dim myFldr As Folder ‘Declare sub folder objects delFlag = False If mytoplvl.Count <> 0 Then Debug.Print “Analyzing: “ & mytoplvl.GetFirst.Name & ” delFlag:” & delFlag For Each myFldr In mytoplvl ‘Sweep through each folder under the inbox If myFldr.Items.Count < 1 Then ‘If the folder is empty check for subfolders If myFldr.Folders.Count < 1 Then ‘If the folder contains not sub folders confirm deletion Debug.Print myFldr.Name & ” contains no items and no subfolders, and will be deleted.” myFldr.Delete ‘Delete the folder delFlag = True Else ‘Folder contains sub folders so confirm deletion FolderPurge myFldr.Folders, delFlag End If Else ‘Folder contains items or (subfolders that may be empty). FolderPurge myFldr.Folders, delFlag End If Next myFldr Else Debug.Print “The folder does not contain any sub folders” & ” delFlag:” & delFlag End If End Sub |
testest test