none
Add new user account and mailbox

    Question

  • Hi,

    I'm trying to create new user account using powershell.

    import-module activedirectory
    Import-Module MSOnline
    
    $LiveCred = Get-Credential
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic –AllowRedirection
    Import-PSSession $Session
    Connect-MsolService -credential $LiveCred
    $username = Read-Host "Enter new user id";
        $fname = Read-Host "Enter Firstname of new user";
        $sname = Read-Host "Enter surname of new user";
    
        $database="databse";
        $password= "password!";
    
        $password= (ConvertTo-SecureString -String $password -AsPlainText -Force)
        $mailboxType = "UserMailbox";
    
        $RoleAssignmentPolicy = "Default Role Assignment Policy"; 
        $SharingPolicy        = "Default Sharing Policy";
    
    
        New-Mailbox –UserPrincipalName "$username@domain.com" -Alias "$fname.$sname" –Database $database -Name "$fname $sname" -OrganizationalUnit Users -Password $password -FirstName $fname –LastName $sname -DisplayName "$fname $sname"  -RecipientType $mailboxType  -RoleAssignmentPolicy $RoleAssignmentPolicy -SharingPolicy $SharingPolicy –ResetPasswordOnNextLogon $true
    

    When I run this script I get this error message.

    A parameter cannot be found that matches parameter name '–UserPrincipalName'.

        + CategoryInfo          : InvalidArgument: (:) [New-Mailbox], ParameterBindingException

        + FullyQualifiedErrorId : NamedParameterNotFound,New-Mailbox

        + PSComputerName        : dddd.outlook.com

    As far I know this should work, i can't seem to see why this might be failing.

    Friday, July 05, 2013 7:11 AM

Answers

  • 1- Ok, for the AD and mailbox creation run the following script using the Exchange Management Shell:

    New-Mailbox -Name "Name of employee" -Alias "Alias of the Employee" -OrganizationalUnit "Domain.com/OU"
    -Database "Preferred Database" -UserPrincipalName "employee@domain.com" -SamAccountName "EmployeeAccount" -FirstName "Employee First Name"
    -Initials "Employee Initials" -LastName "Employee Last Name" -ResetPasswordOnNextLogon $true -Archive $true

     

    2- Run DirSync after a while if the command is succesful

    3- Run the following script using the MSOL Powershell Module:

    Set-MsolUserLicense -UserPrincipalName employee@domain.com -AddLicenses “tenantname:AccountSKUId”

    Where “tenantname” will be in our case “domain” and “AccountSKUId” will be the plan we wish to assign.

    The available AccountSKUId are:

    K1: SHAREPOINTDESKLESS EXCHANGE_S_DESKLESS

    K2: SHAREPOINTWAC SHAREPOINTDESKLESS
    EXCHANGE_S_DESKLESS

    P1: MCOLITE SHAREPOINTLITE
    EXCHANGE_L_STANDARD

    E1: MCOSTANDARD SHAREPOINTSTANDARD
    EXCHANGE_S_STANDARD

    E3: OFFICESUBSCRIPTION MCOSTANDARD SHAREPOINTWAC
    SHAREPOINTENTERPRISE EXCHANGE_S_ENTERPRISE

    E4: OFFICESUBSCRIPTION MCOSTANDARD SHAREPOINTWAC
    SHAREPOINTENTERPRISE EXCHANGE_S_ENTERPRISE

    A2: SHAREPOINTWAC_EDU MCOSTANDARD
    SHAREPOINTSTANDARD_EDU EXCHANGE_S_STANDARD

    Besides, we can run the following cmdlet to determine the AccountSKUId used on our Office365:

    Get-MsolAccountSku | Format-Table AccountSkuId, SkuPartNumber

     

    I hope it helps ;)


    Alberto Pascual MVP-MCSA-MCITP-MCTS-MCP-O365MS-MCC http://blogs.itpro.es/guruxp

    Wednesday, July 10, 2013 12:37 PM
  • you have two choices, tue first one is to put this on your script right before the on premises part:

    add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010

    the other one is to launch the ps1 used by Exchange management Shell right before the on premises part of the script:

    . 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'

    Connect-ExchangeServer -auto

    (note the dot at the beginning of the line)

    the difference between one and the other is that the first option is not "recommended" by Microsoft and the second one has to be executed using a 64bit powershell.


    Alberto Pascual MVP-MCSA-MCITP-MCTS-MCP-O365MS-MCC http://blogs.itpro.es/guruxp

    • Marked as answer by LalaJee Friday, July 12, 2013 9:45 AM
    Wednesday, July 10, 2013 4:17 PM
  • of course, simply execute the command:

    Remove-PSSession $Session

    and if you want to close the window, simply add "exit" to the end of the script after the Remove-PSSession command.


    Alberto Pascual MVP-MCSA-MCITP-MCTS-MCP-O365MS-MCC http://blogs.itpro.es/guruxp

    • Marked as answer by LalaJee Friday, July 12, 2013 9:45 AM
    Wednesday, July 10, 2013 4:19 PM
  • Hi, Thank you so much, its working fine i just create a milbox and user.

    if you dont include 'RecipientType' it set the mailbox by default to user. you can defined other 'RecipientType' e.g. -room, -shared, -equriment.

    Once again thank you. you help is really great.

    • Marked as answer by LalaJee Monday, November 11, 2013 9:49 AM
    Friday, July 12, 2013 9:45 AM

All replies

  • Hi, that´s because the -UserPrincipalName is no longer supported, the right syntax would be:

    New-Mailbox -Alias hollyh -Name hollyh -FirstName Holly -LastName Holt -DisplayName "Holly Holt" -MicrosoftOnlineServicesID hollyh@corp.contoso.com -Password (ConvertTo-SecureString -String 'P@ssw0rd' -AsPlainText -Force) -ResetPasswordOnNextLogon $true

    Regards.

    Alberto Pascual


    Alberto Pascual MCSA-MCITP-MCTS-MCP-O365MS-MCC http://blogs.itpro.es/guruxp

    Friday, July 05, 2013 9:30 AM
  • Script creates a ad account and mail account.
    Friday, July 05, 2013 9:41 AM
  • No I get this error.

    A parameter cannot be found that matches parameter name 'Database'.

        + CategoryInfo          : InvalidArgument: (:) [New-Mailbox], ParameterBindingException

        + FullyQualifiedErrorId : NamedParameterNotFound,New-Mailbox

        + PSComputerName        : ddd.outlook.com

    Friday, July 05, 2013 10:56 AM
  • I know but i believe you have to do it with two different cmdlets, one for the Ad and another for the mailbox because the New-Mailbox syntax doesn´t allow you to specify things like Database or UserPrincipalName.


    Alberto Pascual MCSA-MCITP-MCTS-MCP-O365MS-MCC http://blogs.itpro.es/guruxp

    Friday, July 05, 2013 12:56 PM
  • Do you know which cmdlets i need to do this with.

    Can you please provide a example.

    Tuesday, July 09, 2013 6:39 AM
  • hi, take a look at this article, i'm pretty sure you'll find it useful:

    http://www.powershellmagazine.com/2012/04/23/provisioning-and-licensing-office-365-accounts-with-powershell/

    let me know if it doesn't.

    regards.


    Alberto Pascual MCSA-MCITP-MCTS-MCP-O365MS-MCC http://blogs.itpro.es/guruxp

    Tuesday, July 09, 2013 7:26 AM
  • I have try to rewite the code to somthing like this.

    New-MsolUser-UserPrincipalName'$username@domain.com'-DisplayName'$fname $sname'-Name'$fname $sname'-FirstName'$fname'-LastName'$sname'-UsageLocation'$country'-Password$password-LicenseAssignmentlab:EXCHANGEENTERPRISE-ForceChangePassword$true

    The thing is that i'm missing some of the information like

    -Database$database-RetentionPolicy'Default Archive and Retention Policy'-ActiveSyncMailboxPolicy'Default'

    I need to tell it to which database to go in and apply some of the policy. also need to say which ad ou to go into.

    Tuesday, July 09, 2013 8:15 AM
  • One question, what is exactly your scenario? is it cloud based, or is it hibryd? do you use DirSync?


    Alberto Pascual MCSA-MCITP-MCTS-MCP-O365MS-MCC http://blogs.itpro.es/guruxp

    Tuesday, July 09, 2013 11:18 AM
  • We create account a account locally onto our hibryd server then we use DirSync after few minutes then we move the account onto cloud (exchnage online- office 365)

    Once we hvae move the account onto clude then we licence this account.

    at the moment all the process is manully done, i'm trying to write a script which does this all process automatically.

    Things which are already done

    - I can licence the mailbox using powershell

    - I can copy other account security groups.

    - i can set the office location and other fileds in ad.

    - i can do remote move for mailbox using powershell

    the thing i can't do is the create user and mailbox.

    Tuesday, July 09, 2013 12:36 PM
  • ok now i see... well due that the source of users is your local AD-on premises Exchange server, you must execute the commands in your local server and not on the office365 side.

    i'll explain better, the whole connection chain for example is useless for your scenario as DirSync will do the sync of the new/deleted users.

    but what you still have to do is assign a license to the new users, o would do it on separate scripts, one for the local server and one for the license assignment on the office365 server.

    if you want to, o can generate some examples for you 😉

    regards.


    Alberto Pascual MCSA-MCITP-MCTS-MCP-O365MS-MCC http://blogs.itpro.es/guruxp

    Tuesday, July 09, 2013 6:51 PM
  • If you can please do provide example where i can create this account and then move to exchnage online.

    If its possible i like to copy another account and then create mailbox for this new account.

    Wednesday, July 10, 2013 6:45 AM
  • 1- Ok, for the AD and mailbox creation run the following script using the Exchange Management Shell:

    New-Mailbox -Name "Name of employee" -Alias "Alias of the Employee" -OrganizationalUnit "Domain.com/OU"
    -Database "Preferred Database" -UserPrincipalName "employee@domain.com" -SamAccountName "EmployeeAccount" -FirstName "Employee First Name"
    -Initials "Employee Initials" -LastName "Employee Last Name" -ResetPasswordOnNextLogon $true -Archive $true

     

    2- Run DirSync after a while if the command is succesful

    3- Run the following script using the MSOL Powershell Module:

    Set-MsolUserLicense -UserPrincipalName employee@domain.com -AddLicenses “tenantname:AccountSKUId”

    Where “tenantname” will be in our case “domain” and “AccountSKUId” will be the plan we wish to assign.

    The available AccountSKUId are:

    K1: SHAREPOINTDESKLESS EXCHANGE_S_DESKLESS

    K2: SHAREPOINTWAC SHAREPOINTDESKLESS
    EXCHANGE_S_DESKLESS

    P1: MCOLITE SHAREPOINTLITE
    EXCHANGE_L_STANDARD

    E1: MCOSTANDARD SHAREPOINTSTANDARD
    EXCHANGE_S_STANDARD

    E3: OFFICESUBSCRIPTION MCOSTANDARD SHAREPOINTWAC
    SHAREPOINTENTERPRISE EXCHANGE_S_ENTERPRISE

    E4: OFFICESUBSCRIPTION MCOSTANDARD SHAREPOINTWAC
    SHAREPOINTENTERPRISE EXCHANGE_S_ENTERPRISE

    A2: SHAREPOINTWAC_EDU MCOSTANDARD
    SHAREPOINTSTANDARD_EDU EXCHANGE_S_STANDARD

    Besides, we can run the following cmdlet to determine the AccountSKUId used on our Office365:

    Get-MsolAccountSku | Format-Table AccountSkuId, SkuPartNumber

     

    I hope it helps ;)


    Alberto Pascual MVP-MCSA-MCITP-MCTS-MCP-O365MS-MCC http://blogs.itpro.es/guruxp

    Wednesday, July 10, 2013 12:37 PM
  • Hi,

    The new-mailbox command does not existed. however if i log onto online i do get this command but i dont to do this as i like to create account locally and then more it to exchnage online.

    e.g this is what i have done.

    open cmd

    powershell

    and then run new-mailbox

    this is what i get

    New-Mailbox : The term 'New-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:1
    + New-Mailbox
    + ~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (New-Mailbox:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    PS Z:\>

    Wednesday, July 10, 2013 12:43 PM
  • Hi, as i said, you need to run the command using the EXCHANGE MANAGEMENT SHELL ;) that way you'll have the New-Mailbox available.

    Alberto Pascual MVP-MCSA-MCITP-MCTS-MCP-O365MS-MCC http://blogs.itpro.es/guruxp

    Wednesday, July 10, 2013 12:46 PM
  • is they any import libery i can do. can i just right click on script and run it without run it from exchange shell.
    Wednesday, July 10, 2013 1:21 PM
  • Do you know how i can close this session?

    If I open session from this is they anyway i can close this once i have finish it with it.

    $LiveCred = Get-Credential
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic –AllowRedirection
    Import-PSSession $Session
    Connect-MsolService -credential $LiveCred

    Wednesday, July 10, 2013 1:36 PM
  • you have two choices, tue first one is to put this on your script right before the on premises part:

    add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010

    the other one is to launch the ps1 used by Exchange management Shell right before the on premises part of the script:

    . 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'

    Connect-ExchangeServer -auto

    (note the dot at the beginning of the line)

    the difference between one and the other is that the first option is not "recommended" by Microsoft and the second one has to be executed using a 64bit powershell.


    Alberto Pascual MVP-MCSA-MCITP-MCTS-MCP-O365MS-MCC http://blogs.itpro.es/guruxp

    • Marked as answer by LalaJee Friday, July 12, 2013 9:45 AM
    Wednesday, July 10, 2013 4:17 PM
  • of course, simply execute the command:

    Remove-PSSession $Session

    and if you want to close the window, simply add "exit" to the end of the script after the Remove-PSSession command.


    Alberto Pascual MVP-MCSA-MCITP-MCTS-MCP-O365MS-MCC http://blogs.itpro.es/guruxp

    • Marked as answer by LalaJee Friday, July 12, 2013 9:45 AM
    Wednesday, July 10, 2013 4:19 PM
  • I have try it and fail again.

    This is what I have done first open a command line

    enter powershell

    Then Enter

    . 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'

    Connect-ExchangeServer -auto

    after I enter following details but I got error

    $username = Read-Host "Enter new user id";
        $fname = Read-Host "Enter Firstname of new user";
        $sname = Read-Host "Enter surname of new user";
    
        $database="db_name";
        $password= "Pa$$word!";
    
        $password= (ConvertTo-SecureString -String $password -AsPlainText -Force)
        $mailboxType = "UserMailbox";
    
        $RoleAssignmentPolicy = "Default Role Assignment Policy"; 
        $SharingPolicy        = "Default Sharing Policy";

    [PS] Z:\>New-Mailbox –UserPrincipalName "$username@domain.com" -Alias "$fname.$sname" –Database $database -Name "$fname $sname" -Password $password -FirstName $fname –LastName $sname -DisplayName "$fname $sname"  -RecipientType $mailboxType  -RoleAssignmentPolicy $RoleAssignmentPolicy -SharingPolicy $SharingPolicy –ResetPasswordOnNextLogon $true
    A parameter cannot be found that matches parameter name 'RecipientType'.
        + CategoryInfo          : InvalidArgument: (:) [New-Mailbox], ParameterBindingException
        + FullyQualifiedErrorId : NamedParameterNotFound,New-Mailbox
        + PSComputerName        : ddd.domain.com
     
    
    [PS] Z:\>




    • Edited by LalaJee Thursday, July 11, 2013 11:22 AM added
    Thursday, July 11, 2013 11:18 AM
  • Hi, Thank you so much, its working fine i just create a milbox and user.

    if you dont include 'RecipientType' it set the mailbox by default to user. you can defined other 'RecipientType' e.g. -room, -shared, -equriment.

    Once again thank you. you help is really great.

    • Marked as answer by LalaJee Monday, November 11, 2013 9:49 AM
    Friday, July 12, 2013 9:45 AM
  • Hi, good to know you finally got it working. it's been a pleasure helping you.

    Regards.


    Alberto Pascual MVP-MCSA-MCITP-MCTS-MCP-O365MS-MCC http://blogs.itpro.es/guruxp


    Friday, July 12, 2013 12:02 PM