none
Create a Search Folder with Powershell RRS feed

  • Question

  • Good Morning.

    I need to create a Search Folder in Outlook tosearch mails bigger than 250kb, and I need to do it for all the members of the AD.

    All the scripts and examples i've found, using the EWS Managed API, are with VB or C#.
    I need to do it with Powershell.

    Can anyone help me?

    Thank you.


    Monday, March 28, 2011 9:11 AM

All replies

  • You need to do this a mailbox at a time the script isn't that hard eg to do this for one mailbox

    $MailboxName = "mailbox@domain.com"
    
    $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"
    [void][Reflection.Assembly]::LoadFile($dllpath)
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010)
    $service.TraceEnabled = $false
    
    $windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
    $sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">"
    $aceuser = [ADSI]$sidbind
    #$service.Credentials = New-Object System.Net.NetworkCredential("username","pass")
    $service.AutodiscoverUrl($MailboxName ,{$true})
    
    $svFldid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SearchFolders,$MailboxName)
    $SearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsGreaterThan([Microsoft.Exchange.WebServices.Data.ItemSchema]::Size, "256000");
    $SearchFolder = new-object Microsoft.Exchange.WebServices.Data.SearchFolder($service);
    $searchFolder.SearchParameters.RootFolderIds.Add([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot);
    $searchFolder.SearchParameters.Traversal = [Microsoft.Exchange.WebServices.Data.SearchFolderTraversal]::Deep;
    $searchFolder.SearchParameters.SearchFilter = $SearchFilter
    $searchFolder.DisplayName = "Bigger then blah";
    $searchFolder.Save($svFldid);
    

    You'll want to spend some time to develop proper logic such as only creating a new folder where one doesn't exist etc etc 

    Cheers
    Glen

    Tuesday, March 29, 2011 2:09 AM
  • Good Morning!

    First of all, thank you very much.

    Now, I'm editing your script to apply it for all users and some other things.

    I will post the final script when i have it.

    Cheers
    Santi

    Tuesday, March 29, 2011 10:37 AM
  • Hello again.

    I'm having problems trying to see all the search folders I have.

    I can't find the function that allows to create a view to see the folders (for modify and delete next).

    Any ideas?

    Thanks.

    Tuesday, March 29, 2011 11:08 AM
  • You need to use a findfolders operation if you do a shallow traversal of the WellKnownFolderName.SearchFolder folder that should be enough. What i would be careful of is i wouldn't modify search folders created in Outlook with EWS as EWS only supports a subset of the SearchFolder criteria that Outlook(MAPI) supports so you should only really modify Search folders that you created with EWS in EWS (if that makes sense).

    you search should be something like

    $svFldid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SearchFolders,$MailboxName)
    $fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1000)
    $folders = $service.findfolders($svFldid,$fvFolderView)
    foreach($folder in $folders){
    	$folder.displayName
    }
    
    Cheers
    Glen
    Wednesday, March 30, 2011 2:07 AM
  • Good Morning!

    Thank you for your help, I finally finished my script :)

    That's the final result:

    $MailboxName = "user@domain"
    $dllpath = "C:\Archivos de programa\Microsoft\Exchange\Web Services\1.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.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials("user","password","domain")
    $service.AutodiscoverUrl($MailboxName)
    $service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId;([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
    $svFldid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SearchFolders,$MailboxName)

    $delFldid = [Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete

    $fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1000)

    $folders = $service.findfolders($svFldid,$fvFolderView)

    foreach ($folder in $folders)
    {
        $folder.displayName
        if ($folder.displayName -eq "Big Mail")
        {
             $folder.Delete($delFldid)
        }
    }

    $SearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsGreaterThan([Microsoft.Exchange.WebServices.Data.ItemSchema]::Size, "256000")
    $SearchFolder = new-object Microsoft.Exchange.WebServices.Data.SearchFolder($service)
    $searchFolder.SearchParameters.RootFolderIds.Add([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot);
    $searchFolder.SearchParameters.Traversal = [Microsoft.Exchange.WebServices.Data.SearchFolderTraversal]::Deep;
    $searchFolder.SearchParameters.SearchFilter = $SearchFilter
    $searchFolder.DisplayName = "Big Mail";
    $searchFolder.Save($svFldid);


    Wednesday, March 30, 2011 7:22 AM
  • Hello again.

    Only one question. When you list all the search folders you have in your inbox folder, can you see the search properties of them?

    For example, if you have a search folder called Huge Mail, can you see that finds bigger mails than 1MB? How?

    Thank you!

    Thursday, March 31, 2011 6:33 AM
  • You can bind to an existing SearchFolder once you know the id and then display its search parameters eg

    $svFldid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SearchFolders,$MailboxName)
    $fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1000)
    $folders = $service.findfolders($svFldid,$fvFolderView)
    foreach($folder in $folders){
    	if ($folder.displayName -eq "Bigger then blah"){
    		$SearchFolder = [Microsoft.Exchange.WebServices.Data.SearchFolder]::Bind($service,$folder.Id)
    		$SearchFolder.SearchParameters.SearchFilter
    	}
    }
    

    Cheers
    Glen

    Friday, April 1, 2011 1:47 AM
  • Hello Again,

    When I try to change the searchfolder filter properties, it founds and error.

    Could you help me?

    Part of the CODE -----------------------------------------------------------------------------------------------------------

    foreach($folder in $folders)
    {
     $SearchFolder = [Microsoft.Exchange.WebServices.Data.SearchFolder]::Bind($service,$folder.Id)
     #$folder.displayName
     if (($SearchFolder.SearchParameters.SearchFilter.Value) -cge 100000)
     {
      $SearchFolder.SearchParameters.SearchFilter.Value = "250000"
      $SearchFolder.Save($folder);
     }
    }

    ERRORS: --------------------------------------------------------------------------------------------------

    Cannot find an overload for "Save" and the argument count: "1".
    At C:\Documents and Settings\steixidor\Mis documentos\cercaoutlook.ps1:37 char:21
    +         $SearchFolder.Save <<<< ($folder);
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest
     
    Cannot find an overload for "Save" and the argument count: "1".
    At C:\Documents and Settings\steixidor\Mis documentos\cercaoutlook.ps1:37 char:21
    +         $SearchFolder.Save <<<< ($folder);
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest

    Thanks

    Monday, April 4, 2011 11:53 AM
  • You need to call the update() method save is only used when creating a folder

    Cheers
    Glen

    Tuesday, April 5, 2011 2:49 AM
  • Do you know which command must I use to know the user's inbox language ¿?

    Thanks :)

    Thursday, April 7, 2011 11:08 AM
  • You need to access the PR_LOCALE_ID extended property but unfortunately you can't access this using EWS my suggestion would be you can access the Inbox regardless of language in EWS and just check the DisplayName of the Inbox folder that should return the localised name of the Inbox if its using a different language of Inbox if it using english.

    Cheers
    Glen

    Friday, April 8, 2011 7:26 AM
  • Finally, I used this:

    get-mailbox | select-object languages

    And it returns, for example, {en-EN} for English, {zt-CN} for Chinese, {es-ES} for Spanish,...

    (Here, at work, we have a library that allow that, but I don't remember the name. I think we downloaded it free on the NET)

    But now, I have a problem.

    For the results of the query get-mailbox | select-object primarysmtpaddress (which returns all the email adresses of the users) I must call the function that creates the search folder (called 'createsearchfolder($Mailboxname)'). Do you know how must I create the loop?

    I tried this, but it doesn't work.

    foreach ($item in (get-mailbox | select-object primarysmtpaddress)) {crearcarpeta -MailboxName $_.primarysmtpaddress

    Thanks!

    Friday, April 8, 2011 11:39 AM
  • You should just be able to do foreach($mbx in Get-Mailbox){ $mbx.PrimarySmtpAddress}

    Cheers
    Glen

    Monday, April 11, 2011 1:39 AM
  • Good Morning!

    My script is finally working!
    However, I need that, given an e-mail address, if it detects that it's a resource (for example, Equipment Mailbox and Room Mailbox), it doesn't create the folder.

    Do you know how can I perform this detection? That is, how can I know if it is an ordinary user or a resource?

    Thank you very much, again :)


    Tuesday, April 12, 2011 8:03 AM
  • Hello again, I've found it!

    [PS] C:\>Get-Mailbox Resource1 |fl *resource*

    IsResource : True

    ResourceType : Room

     Cheers.

    Tuesday, April 12, 2011 9:26 AM
  • I know this thread is very old by now, but is there any chance you are still active here and have the final script available? I have a similar issue but can't get it to work right now.

    Tuesday, February 19, 2019 10:17 AM