# PowerShell find UPN of users in the given OU and create Exchange Mailbox with the alias == UPN # If the mailbox already exists, the script errors on that entry. # Author: Jonathan Haddock, following an example PS foreach script from Guy Thomas # (http://www.computerperformance.co.uk/powershell/powershell_active_directory.htm) # Version 1, August 2010, tested with WinSvr 2008 R2 Stnd and Exchange 2010 Stnd $Dom = 'LDAP://OU=Dummy;OU=Students;OU=User;DC=bcgs;DC=local' $Root = New-Object DirectoryServices.DirectoryEntry $Dom $i=0 # Create a selector and start searching from the Root of AD $selector = New-Object DirectoryServices.DirectorySearcher $selector.SearchRoot = $root $adobj= $selector.findall() |` where {$_.properties.objectcategory -match "CN=Person"} foreach ($person in $adobj){ $prop=$person.properties $i++ # In AD the property is userPrincipalName however, PS makes the attribute lower case, hence userprincipalname $upn = $prop.userprincipalname[0] # The UPN contains @ followed by the suffix, we only want the UPN prefix so we use split: $upnsplit = $upn.split("@") $alias = $upnsplit[0] # Output the alias to the screen: Write-host $alias # Enable the exchange mailbox Enable-Mailbox -Identity "$upn" -Alias "$alias" } "Total $i"