Friday 7 September 2012

PowerShell with Exchange

Here is something new I learnt today.

In order to create a mailbox for an active directory user using powershell we need to Get-PSSnapin for Exchange and Import-Module for Active Directory.

Clear-Host

#SnapIn for Exchange Mailbox
if(-not(Get-PSSnapin | Where-Object {$_.Name -eq "Microsoft.Exchange.Management.PowerShell.E2010"}))
{
  Add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
}

#SanpIn For SharePoint
if(-not(Get-PSSnapin | Where-Object {$_.Name -eq "Microsoft.SharePoint.PowerShell"})) {
  Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

# Add Active Directory Snap-In
Import-Module ServerManager
Add-WindowsFeature RSAT-AD-PowerShell
Import-Module ActiveDirectory

Once this Import is done we can call the "New-Mailbox" command which will create the mailbox and AD user.

This command will create mailbox along with user creation in AD.

New-Mailbox -Name $displayName -Database $dbName  -Password $password -UserPrincipalName $userPrincipalName -Alias $samAccountName  -DisplayName $displayName -FirstName $givenName -LastName $surname -OrganizationalUnit $ouPath    

This command will create user in AD and then you can enable the mailbox

New-ADUser -Enable $True -PasswordNeverExpires $enable -PassThru  -Name $displayName -GivenName $givenName -Surname $surname -DisplayName $displayName -EmailAddress $emailAddress -SamAccountName $samAccountName -Title $title -Department $department  -UserPrincipalName $userPrincipalName -Path $path -AccountPassword $password

Enable-Mailbox $userPrincipalName –Database $dbName

Hope this help.
Isha
 

No comments:

Post a Comment