Get-NewMailboxes function

I am working on a script to modify all new mailboxes created within the last X days. Plan to use this to apply mailbox retention policies to these new mailboxes each evening with a scheduled script. After watching a short web seminar on powershell.com, I thought I'd create it as a function that has some required parameters.

Reference: http://www.wisesoft.co.uk/scripts/powershell_search_for_all_users_by_the...

function Get-NewMailboxes {
        <#
        .Synopsis Return all mailboxes created after a specific number of days back
       
        #>
        param (
        [parameter(Mandatory = $true, ValueFromPipeline=$true)]
        [int]$DaysBack
        )
        if ($daysback -gt 0) {$daysBack = $daysback * -1}
       
        #All Mailboxes created in the last 24 hours.
        $searchDate = (Get-Date).AddDays($DaysBack) | Get-Date -UFormat "%Y%m%d000000.0Z"
        $strFilter = "(&(objectCategory=User)(objectclass=user)(whencreated>=$searchDate))"

        $objDomain = New-Object System.DirectoryServices.DirectoryEntry
        $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
        $objSearcher.SearchRoot = $objDomain
        $objSearcher.PageSize = 1000
        $objSearcher.Filter = $strFilter
        $objSearcher.SearchScope = "Subtree"

        #$colProplist = "distinguishedname"
        #foreach ($i in $colPropList){
        $objSearcher.PropertiesToLoad.Add("distinguishedname")  | out-Null
        #}

        $colResults = $objSearcher.FindAll()
    $mbxes = @()
        foreach ($objResult in $colResults) {
                $objItem = $objResult.Properties;      
                $mbxes += Get-mailbox -resultsize unlimited -id $objitem.distinguishedname[0].ToString()
        }
        return $mbxes
}

Use:

get-newMailboxes -daysBack 1 | set-casmailbox -activesyncEnabled $false

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <blockquote> <center> <hr> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <drupal6>, <html>, <java>, <javascript>, <php>, <posh>.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.