{"id":33,"date":"2012-10-27T12:35:53","date_gmt":"2012-10-27T11:35:53","guid":{"rendered":"http:\/\/powerkjell.com\/?p=33"},"modified":"2013-02-24T19:21:42","modified_gmt":"2013-02-24T18:21:42","slug":"create-office-365-accounts-using-powershell","status":"publish","type":"post","link":"https:\/\/powerkjell.com\/?p=33","title":{"rendered":"Create Office 365 accounts using Powershell"},"content":{"rendered":"<p>When Office 365 was released, provisioning accounts with <a title=\"Office 365 DirSync Content Map\" href=\"http:\/\/community.office365.com\/en-us\/wikis\/sso\/office-365-dirsync-content-map.aspx\" target=\"_blank\">DirSync<\/a> was the only available method. There was no support for Powershell which was unfortunate. Since then Microsoft has released <a title=\"Use Windows PowerShell to manage Office 365\" href=\"http:\/\/onlinehelp.microsoft.com\/en-us\/office365-enterprises\/hh124998.aspx\" target=\"_blank\">Powershell support <\/a>and readily <a title=\"Windows PowerShell cmdlets for Office 365\" href=\"http:\/\/onlinehelp.microsoft.com\/en-us\/office365-enterprises\/hh125002.aspx\" target=\"_blank\">documentation<\/a>. This article describes a quick way to create an account in Office 365 using Powershell.<\/p>\n<p>To begin using the Office\u00a0365 cmdlets, you first need to install them. This is described under section <a title=\"Use Windows PowerShell to manage Office 365\" href=\"http:\/\/onlinehelp.microsoft.com\/en-us\/office365-enterprises\/hh124998.aspx\" target=\"_blank\">Install the Office 365 cmdlets<\/a>. Use x64 and make sure you set ExecutionPolicy to RemoteSigned.<\/p>\n<p>[powershell]Set-ExecutionPolicy RemoteSigned[\/powershell]<\/p>\n<p>Then you need to import Powershell module for Office 365.<\/p>\n<p>[powershell]Import-Module MSOnline[\/powershell]<\/p>\n<p>Create a session.<\/p>\n<p>[powershell]$Username = &#8220;admin@mydomain.onmicrosoft.com&#8221;<br \/>\n$Password = ConvertTo-SecureString &#8220;P@ssword&#8221;\u00a0-AsPlainText -Force<br \/>\n$Credentials = New-Object System.Management.Automation.PSCredential $Username,$Password<br \/>\nConnect-MsolService -Credential $Credentials[\/powershell]<\/p>\n<p>Create a new User.<\/p>\n<p>[powershell]New-MsolUser -UserPrincipalName johndoe@mydomain.onmicrosoft.com -DisplayName &#8220;John Doe&#8221; -FirstName &#8220;John&#8221; -LastName &#8220;Doe&#8221;[\/powershell]<\/p>\n<p>New-MsolUser will create a random password and return a MsolUser object which contains the new password. However, when logging in to the portal, user will face an error message.<\/p>\n<p><a href=\"http:\/\/powerkjell.com\/wp-content\/uploads\/2012\/10\/NotLicensed.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-39\" title=\"NotLicensed\" src=\"http:\/\/powerkjell.com\/wp-content\/uploads\/2012\/10\/NotLicensed-300x152.png\" alt=\"\" width=\"300\" height=\"152\" srcset=\"https:\/\/powerkjell.com\/wp-content\/uploads\/2012\/10\/NotLicensed-300x152.png 300w, https:\/\/powerkjell.com\/wp-content\/uploads\/2012\/10\/NotLicensed-500x254.png 500w, https:\/\/powerkjell.com\/wp-content\/uploads\/2012\/10\/NotLicensed.png 758w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>This is beacuse a user needs to be assigned to a subscription. To do this UsageLocation nedds to be set and second a license added:<\/p>\n<p>[powershell]Set-MsolUser -UserPrincipalName johndoe@mydomain.onmicrosoft.com -UsageLocation &#8220;US&#8221;<br \/>\nSet-MsolUserLicense -UserPrincipalName johndoe@mydomain.onmicrosoft.com -AddLicenses &#8220;tengil:ENTERPRISEPACK&#8221;[\/powershell]<\/p>\n<p>It is possible (and recommended) to create a user with a license in a single call.<\/p>\n<p>[powershell]New-MsolUser -UserPrincipalName johndoe@mydomain.onmicrosoft.com -DisplayName &#8220;John Doe&#8221; -FirstName &#8220;John&#8221; -LastName &#8220;Doe&#8221; -UsageLocation &#8220;US&#8221; -LicenseAssignment &#8220;Contoso:BPOS_Standard&#8221;[\/powershell]<\/p>\n<p>A\u00a0simple script to summarize this:<\/p>\n<p>[powershell]<\/p>\n<p>param(<br \/>\n[parameter(Mandatory = $true)][string]$AdminUsername,<br \/>\n[parameter(Mandatory = $true)][string]$AdminPassword,<br \/>\n[parameter(Mandatory = $true)][string]$UserPrincipalName,<br \/>\n[parameter(Mandatory = $true)][string]$DisplayName<br \/>\n[parameter(Mandatory = $true)][string]$FirstName,<br \/>\n[parameter(Mandatory = $true)][string]$LastName,<br \/>\n[parameter(Mandatory = $true)][string]$UsageLocation,<br \/>\n[parameter(Mandatory = $true)][string]$LicenseAssignment<br \/>\n)<\/p>\n<p>function New-O365User()<br \/>\n{<br \/>\nif((Get-Module MSOnline) -eq $null)<br \/>\n{<br \/>\nImport-Module MSOnline<br \/>\n}<\/p>\n<p>$pwd = ConvertTo-SecureString $AdminPassword -AsPlainText -Force<br \/>\n$Credentials = New-Object System.Management.Automation.PSCredential $AdminUsername,$pwd<br \/>\nConnect-MsolService -Credential $Credentials<\/p>\n<p>New-MsolUser -UserPrincipalName $UserPrincipalName -DisplayName $DisplayName -FirstName $FirstName -LastName $LastName -UsageLocation $UsageLocation -LicenseAssignment $LicenseAssignment<\/p>\n<p>}<\/p>\n<p>New-O365User -AdminUsername $AdminUsername -AdminPassword $AdminPassword -UserPrincipalName $UserPrincipalName -DisplayName $DisplayName -FirstName $FirstName -LastName $LastName -UsageLocation $UsageLocation -LicenseAssignment $LicenseAssignment<\/p>\n<p>[\/powershell]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When Office 365 was released, provisioning accounts with DirSync was the only available method. There was no support for Powershell which was unfortunate. Since then Microsoft has released Powershell support and readily documentation. This article describes a quick way to &hellip; <a href=\"https:\/\/powerkjell.com\/?p=33\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[6,17],"_links":{"self":[{"href":"https:\/\/powerkjell.com\/index.php?rest_route=\/wp\/v2\/posts\/33"}],"collection":[{"href":"https:\/\/powerkjell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/powerkjell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/powerkjell.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/powerkjell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=33"}],"version-history":[{"count":14,"href":"https:\/\/powerkjell.com\/index.php?rest_route=\/wp\/v2\/posts\/33\/revisions"}],"predecessor-version":[{"id":95,"href":"https:\/\/powerkjell.com\/index.php?rest_route=\/wp\/v2\/posts\/33\/revisions\/95"}],"wp:attachment":[{"href":"https:\/\/powerkjell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/powerkjell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/powerkjell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}