Powershell EWS API - Hide folder RRS feed

  • Question

  • I've used Powershell with EWS API to create and delete folders, but struggling to get some working code for making a folder hidden.

    The folder is at the root of the mailbox "/Folder1", but ideally I'd like to be able to hide the folder at any given folder path e.g. "Inbox/Folder1"

    Any good examples of how to achieve that?

    The code I've been using searches for the folder by DisplayName, and uses a PathtoSearch parameter ,eg. "/Folder1"


            # 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++){

                # 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
                    Write-Host "Folder does not exist in Mailbox:" $MailboxName -f $warning
                    Add-Content $LogFile ("Folder does not exist in Mailbox:" + $MailboxName)
                    $tfTargetFolder = $null

            ForEach($folder in $findFolderResults.Folders)
                $isHiddenProp = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x10f4,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Boolean)
                $PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
                $tfTargetFolder = $folder
                $oFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$tfTargetFolder.Id,$PropertySet)
                Write-Host "Hiding Folder     : $folder : $tfTargetFolder.Id`n" -f $warning
                $folder.SetExtendedProperty($isHiddenProp, $true)
                Add-Content $LogFile ("Hidden,$folder,$tfTargetFolder.Id")

    If there is more than one folder with same name, would this break the code above? I am wanting to find a particular folder at the root, but want it to stop there - I don't want it hidding every folder named "Folder1"

    I'd thought about just passing the folder Id in of the specific folder (instead of DisplayName).

    I'm looking to hide at the root level, how would I adapt the folder search to use that FolderId if possible (retreived from a get-mailboxstatistics command against all the affected mailboxes)

    Friday, January 5, 2018 5:36 PM

All replies

  • If you only want to return folders at the root level the set the folder traversal to Shallow eg a Shallow traversal will only return the child folders of the current folder your searching, a Deep Traversal will return all child, grandchild etc folders.

    $fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Shallow;  


    Sunday, January 7, 2018 10:23 PM