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”
@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.
@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`?
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…
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’
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"
}