Deleting all subfolders in a specific path in outlook

Welcome Forums General PowerShell Q&A Deleting all subfolders in a specific path in outlook

This topic contains 5 replies, has 4 voices, and was last updated by

 
Participant
1 month ago.

  • Author
    Posts
  • #119821

    Participant
    Points: 26
    Rank: Member

    Hi – background; I need to iterate through all mailboxes and delete anything under subfolder '/inbox/_dm*' this will be a little over 2,500 mailboxes..

    In essence this script works until I do a $folder and get no display names only unique folder ID's, so when I ask the script to look for anything /inbox/_dm* it comes back with nothing.  I've also tried to change the search from FolderPath to displayname -like '_dm*' and still nothing..

    the last part is the iteration piece, I've tried creating an array of all the mailboxes and then iterating through the script but it returns an error 'cannot find an overload for "folderid"

    Id : AAMkADVjZjYxYzBmLTQ5ZjEtNGNjNy1iMjRkLTFhZjViZDhjMTVhNAAuAAAAAAAylrfphVrrT75bGX8Laz+0AQC/Cp0XtLwATJRD2iBNkmHtAAAc/4AHAAA=
    ParentFolderId : AAMkADVjZjYxYzBmLTQ5ZjEtNGNjNy1iMjRkLTFhZjViZDhjMTVhNAAuAAAAAAAylrfphVrrT75bGX8Laz+0AQBf66Ta7aOkRKSDNOB4ibBkAAAAnG3wAAA=
    ChildFolderCount : 3
    DisplayName : _DM Quick Filer
    FolderClass : IPF.Note
    TotalCount : 0
    ExtendedProperties : {}
    ManagedFolderInformation : 
    EffectiveRights : CreateAssociated, CreateContents, CreateHierarchy, Delete, Modify, Read, ViewPrivateItems
    Permissions : {}
    UnreadCount : 0
    PolicyTag : 
    ArchiveTag : 
    WellKnownFolderName : 
    Schema : {Id, ParentFolderId, FolderClass, DisplayName...}
    Service : Microsoft.Exchange.WebServices.Data.ExchangeService
    IsNew : False
    IsDirty : False

     

     

    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchangeserver/PowerShell/ -Authentication Kerberos
    Import-PSSession $Session -DisableNameChecking:$disablenamechecking
    $user = "username@domain.com"
    $mailboxes = get-mailbox -organizationalunit "/Users" | select name
    $mbx = "username@domain.com"
    
    Import-Module "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
    $Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2)
    $Service.AutodiscoverUrl($user,{$true})
    
    #Impersonating $mailbox
    $service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $mbx)
    
    #Foreach ($mbx in $mailboxes) {
    $RootFolderName = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$mbx)
    $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
    #$psPropertySet.Add($PR_Folder_Path);
    $RootFolderd = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service,$RootFolderName)
    $FolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
    $FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
    $Folders = $RootFolderd.FindFolders($FolderViewSet)
    
    $folder = $Folders | ? {$_.Folderpath -like "/inbox/_DM*"}
    $folder
    
    #Assuming you've identified the folder you want to delete
    $folder.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete)
    
    # }
  • #119913

    Participant
    Points: 832
    Helping Hand
    Rank: Major Contributor

    @marcus, I saw your post in "Read Me Before Posting! You'll be Glad You Did!" thread, can you please clean it, Moderator will remove it. But let them know about the reposting here as part of clean up.

  • #120766

    Moderator
    Points: 100
    Team MemberHelping Hand
    Rank: Participant

    All cleaned up.

    @OP: I'm afraid I've not got a tame Exchange server to play with – I'd assume there's an issue with your call on line 15. Do you get any useful information on the available constructors by doing `[Microsoft.Exchange.WebServices.Data.FolderId]::new`?

  • #120954

    Participant
    Points: 26
    Rank: Member

    Hi James, thanks for looking in.. I do get output back when I manually put in the variables as you can see below.. the problem is that the 'Display Name' of the folders aren't there...

    PS D:\scripts\marcus> Import-Module "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
    $Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2)
    $Service.AutodiscoverUrl($user,{$true})
    
    #Impersonating $mailbox
    $service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $mbx)
    
    #Foreach ($mbx in $mailboxes) {
    $RootFolderName = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$mbx)
    $RootFolderd = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service,$RootFolderName)
    $FolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
    $FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
    $Folders = $RootFolderd.FindFolders($FolderView)
    
    PS D:\scripts\marcus> $RootFolderName
    
    FolderName Mailbox UniqueId ChangeKey
    ---------- ------- -------- ---------
    Root marcus.levy@domain.com.au
    
    PS D:\scripts\marcus> $RootFolderd
    
    
    Id : AAMkADVjZjYxYzBmLTQ5ZjEtNGNjNy1iMjRkLTFhZjViZDhjMTVhNAAuAAAAAAAylrfphVrrT75bGX8Laz+0AQBf66Ta7aOkRKSDNOB4ibBkAAAAnG3sAAA=
    ParentFolderId : AAMkADVjZjYxYzBmLTQ5ZjEtNGNjNy1iMjRkLTFhZjViZDhjMTVhNAAuAAAAAAAylrfphVrrT75bGX8Laz+0AQBf66Ta7aOkRKSDNOB4ibBkAAAAnG3sAAA=
    ChildFolderCount : 21
    DisplayName : 
    FolderClass : 
    TotalCount : 6
    ExtendedProperties : {}
    ManagedFolderInformation : 
    EffectiveRights : CreateAssociated, CreateContents, CreateHierarchy, Delete, Modify, Read, ViewPrivateItems
    Permissions : {Microsoft.Exchange.WebServices.Data.UserId, Microsoft.Exchange.WebServices.Data.UserId}
    UnreadCount : 0
    PolicyTag : 
    ArchiveTag : 
    WellKnownFolderName : 
    Schema : {Id, ParentFolderId, FolderClass, DisplayName...}
    Service : Microsoft.Exchange.WebServices.Data.ExchangeService
    IsNew : False
    IsDirty : False
    
    
    PS D:\scripts\marcus> $FolderView
    
    
    Traversal : Deep
    PageSize : 1000
    OffsetBasePoint : Beginning
    Offset : 0
    PropertySet :

    when I try and return value for $folder there is nothing there.. because $rootfolderd (line 27) does not contain a value for displayname

    PS D:\scripts\marcus> $folder = $Folders | ? {$_.FolderPath -like "/Inbox/_DM*"}
    
    PS D:\scripts\marcus> $folder

    I've tried adding the below piece and it returns unsurprisingly 'cannot call a method on  a null valued expression'

    $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties) 
    $psPropertySet.Add($PR_Folder_Path);

    Any further input greatly appreciated...

  • #120961

    Participant
    Points: 26
    Rank: Member

    Hi James, thanks for looking in.. I do get values back when entering the variables $rootfoldername,$rootfolderd and $folderview.. the problem seems to be  $rootfolderd does not contain 'display name'

    Any further input / ideas very welcome..

    PS D:\scripts\marcus> Import-Module "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
    $Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2)
    $Service.AutodiscoverUrl($user,{$true})
    
    #Impersonating $mailbox
    $service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $mbx)
    
    #Foreach ($mbx in $mailboxes) {
    $RootFolderName = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$mbx)
    $RootFolderd = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service,$RootFolderName)
    $FolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
    $FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
    $Folders = $RootFolderd.FindFolders($FolderView)
    
    PS D:\scripts\marcus> $RootFolderName
    
    FolderName Mailbox UniqueId ChangeKey
    ---------- ------- -------- ---------
    Root mymailbox@domain.com.au (valid mailbox returned)
    
    PS D:\scripts\marcus> $RootFolderd
    
    
    Id : AAMkADVjZjYxYzBmLTQ5ZjEtNGNjNy1iMjRkLTFhZjViZDhjMTVhNAAuAAAAAAAylrfphVrrT75bGX8Laz+0AQBf66Ta7aOkRKSDNOB4ibBkAAAAnG3sAAA=
    ParentFolderId : AAMkADVjZjYxYzBmLTQ5ZjEtNGNjNy1iMjRkLTFhZjViZDhjMTVhNAAuAAAAAAAylrfphVrrT75bGX8Laz+0AQBf66Ta7aOkRKSDNOB4ibBkAAAAnG3sAAA=
    ChildFolderCount : 21
    DisplayName : 
    FolderClass : 
    TotalCount : 6
    ExtendedProperties : {}
    ManagedFolderInformation : 
    EffectiveRights : CreateAssociated, CreateContents, CreateHierarchy, Delete, Modify, Read, ViewPrivateItems
    Permissions : {Microsoft.Exchange.WebServices.Data.UserId, Microsoft.Exchange.WebServices.Data.UserId}
    UnreadCount : 0
    PolicyTag : 
    ArchiveTag : 
    WellKnownFolderName : 
    Schema : {Id, ParentFolderId, FolderClass, DisplayName...}
    Service : Microsoft.Exchange.WebServices.Data.ExchangeService
    IsNew : False
    IsDirty : False
    
    
    PS D:\scripts\marcus> $FolderView
    
    
    Traversal : Deep
    PageSize : 1000
    OffsetBasePoint : Beginning
    Offset : 0
    PropertySet :

     

  • #121473

    Participant
    Points: 316
    Helping Hand
    Rank: Contributor

    Give this a shot on one test user mailbox first, then params/variable as needed, to pipe the list, if it, the direct one works.

    ## Find and Bind to Folder based on Path
    # Define the path to search should be separated with \
    $PathToSearch = "\Inbox\My New Folder123"
    
    # Bind to the MSGFolder Root
    $folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)
    $tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
    
    # Split the Search path into an array
    $fldArray = $PathToSearch.Split("\")
    
    # Loop through the Split Array and do a Search for each level of folder
    for ($lint = 1; $lint -lt $fldArray.Length; $lint++)
    {
    $fldArray[$lint]
    
    # Perform search based on the displayname of each folder level
    $fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1)
    $SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$fldArray[$lint])
    $findFolderResults = $service.FindFolders($tfTargetFolder.Id,$SfSearchFilter,$fvFolderView)
    
    if ($findFolderResults.TotalCount -gt 0)
    {
    foreach($folder in $findFolderResults.Folders)
    {
    $tfTargetFolder = $folder
    }
    }
    else
    {
    "Error Folder Not Found"
    $tfTargetFolder = $null
    break
    }
    }
    if($tfTargetFolder -ne $null)
    {
    
    #Delete the Folder using HardDelete
    $tfTargetFolder.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)
    "Folder Deleted"
    }
    

You must be logged in to reply to this topic.