{"id":870,"date":"2014-08-04T00:01:00","date_gmt":"2014-08-04T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/08\/04\/use-powershell-to-create-bulk-users-for-office-365\/"},"modified":"2022-06-20T14:17:45","modified_gmt":"2022-06-20T21:17:45","slug":"use-powershell-to-create-bulk-users-for-office-365","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-bulk-users-for-office-365\/","title":{"rendered":"Use PowerShell to Create Bulk Users for Office 365"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about creating bulk users in Office 365.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning I spent a bit of time talking to my mentee. It is really cool, because yesterday I spent some time talking to my mentor. So it is sort of like the circle completing itself.<\/p>\n<p>I have been getting quite a few questions about Office 365 and Windows PowerShell here lately, so I thought I would spend a bit of time talking about that.<\/p>\n<p style=\"margin-left:30px\">\n  <b>Note\u00a0<\/b> I have written many Hey, Scripting Guy! Blog posts about this topic, including <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/getting-started-with-office-365-and-powershell\/\" target=\"_blank\" rel=\"noopener\">Getting Started with the Office 365 PowerShell<\/a>, <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-explore-office-365-installation\/\" target=\"_blank\" rel=\"noopener\">Use PowerShell to Explore Office 365 Installation<\/a>, and <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-manage-office-365-users\/\" target=\"_blank\" rel=\"noopener\">Use PowerShell to Manage Office 365 Users<\/a>. For more information about the fundamental tasks related to Office 365 and Windows PowerShell, refer to this entire series of <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/tag\/office-365\/\" target=\"_blank\" rel=\"noopener\">Hey, Scripting Guy! Blog posts<\/a>.\n<\/p>\n<h2>Connecting to Office 365<\/h2>\n<p>The first thing to do is to make the connection to the Office 365 tenant installation.<\/p>\n<p>I first store my credentials in an XML file so I can easily reuse them. When I call the <strong>Get-Credential<\/strong> cmdlet, a dialog box appears. I type my credentials in the box, and they are now stored in an XML file. I can import the credentials by using the <strong>Import-Clixml<\/strong> cmdlet. I use the reconstituted <strong>Credential<\/strong> object to make my connection. Here are the commands I type:<\/p>\n<p style=\"margin-left:30px\">\n  PS C:\\> Get-Credential &#8220;admin@ScriptingGuy.OnMicrosoft.Com&#8221; |\n<\/p>\n<p style=\"margin-left:30px\">\n  >>> Export-Clixml C:\\fso\\ScriptingGuyCredential.xml\n<\/p>\n<p style=\"margin-left:30px\">\n  PS C:\\> $cred = Import-Clixml C:\\fso\\ScriptingGuyCredential.xml\n<\/p>\n<p style=\"margin-left:30px\">\n  PS C:\\> Connect-MsolService -Credential $cred\n<\/p>\n<p>As shown in the following image, no output appears in the Windows PowerShell console:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-4-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-4-14-01.png\" alt=\"Image of command\" title=\"Image of command\" \/><\/a><\/p>\n<p style=\"margin-left:30px\">\n  <b>Note\u00a0<\/b> To update the Azure Active Directory (Azure\u00a0AD) module, you must first uninstall it via the Control Panel. After you do that, you can install it from the TechNet Library: <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/jj151815.aspx#bkmk_installmodule\" target=\"_blank\" rel=\"noopener\">Install the Azure AD Module<\/a>.\n<\/p>\n<h2>Creating a bunch of users via a CSV file<\/h2>\n<p>Today, I am going to create a CSV file and use it to create a bunch of users for my Office 365 installation. I am not going to discuss the license assignment, license options, or usage location assignments. I will look at these tomorrow. For today, I just want to create a bunch of users.<\/p>\n<p>To create a user in Office 365 by using the Azure\u00a0AD module, I use the <strong>New-MSOlUser<\/strong> cmdlet. The syntax for this looks an awfully lot like the <strong>New-User<\/strong> cmdlet in the standard Active Directory module. There are not quite as many options, but the process is very similar. Here is the syntax for the <strong>New-MSOlUser<\/strong> cmdlet:<\/p>\n<p style=\"margin-left:30px\">\n  NAME\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 New-MsolUser\n<\/p>\n<p style=\"margin-left:30px\">\n  SYNOPSIS\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 Adds a new user to Windows Azure Active Directory.\n<\/p>\n<p style=\"margin-left:30px\">\n  SYNTAX\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 New-MsolUser -DisplayName <string> -UserPrincipalName <string>\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 [-AlternateEmailAddresses <string[]>] [-AlternateMobilePhones <string[]>]\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 [-BlockCredential <Boolean>] [-City <string>] [-Country <string>] [-Department\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 <string>] [-Fax <string>] [-FirstName <string>] [-ForceChangePassword <Boolean>]\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 [-ImmutableId <string>] [-LastName <string>] [-LicenseAssignment <string[]>]\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 [-LicenseOptions <LicenseOption[]>] [-MobilePhone <string>] [-Office <string>]\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 [-Password <string>] [-PasswordNeverExpires <Boolean>] [-PhoneNumber <string>]\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 [-PostalCode <string>] [-PreferredLanguage <string>] [-State <string>]\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 [-StreetAddress <string>] [-StrongPasswordRequired <Boolean>] [-TenantId <Guid>]\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 [-Title <string>] [-UsageLocation <string>] [<CommonParameters>]\n<\/p>\n<p>To use the cmdlet, it is as simple as filling in the blanks. Here is a quick example:<\/p>\n<p style=\"margin-left:30px\">\n  New-MsolUser -UserPrincipalName &#8220;ScriptingWife@ScriptingGuy.OnMicrosoft.Com&#8221; -City\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0Charlotte -State NC -Country USA -DisplayName ScriptingWife\n<\/p>\n<p>In this command, I did not assign a password. The cmdlet returns a user object, and that user object includes a password. I could then pipe the information from the returned object to <strong>Send-SMTPMail<\/strong> to send the user information and password to the user. The cool thing is that I do not have to create a password, nor do I have to supply one when I create the new user. Here is the output from the command in the Windows PowerShell console:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-4-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-4-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>When I go to the Office 365 tenant admin site, I can see that the Scripting Wife user has indeed been created:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-4-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-4-14-03.png\" alt=\"Image of webpage\" title=\"Image of webpage\" \/><\/a><\/p>\n<h2>Read a CSV file and create the users<\/h2>\n<p>So now, I want to read a CSV file and create a bunch of users. To do this, first I create a CSV file. I like to use Microsoft Excel for doing this because it provides a good interface, and the menu offers me the option to save as a CSV file.<\/p>\n<p>I was hoping that I could splat the users from the CSV file to the <strong>New-MSOLUser<\/strong> cmdlet, but unfortunately that generates an error. Therefore, I will have to pipe my users via the <strong>Foreach-Object<\/strong> cmdlet. The cmdlet doesn\u2019t accept positional parameters very well either, and so I have to do everything manually. This is shown here:<\/p>\n<p style=\"margin-left:30px\">\n  $users = Import-Csv C:\\fso\\Office365Users.CSV\n<\/p>\n<p style=\"margin-left:30px\">\n  $users | ForEach-Object {\n<\/p>\n<p style=\"margin-left:30px\">\n  New-MsolUser -UserPrincipalName $_.UserPrincipalName -City $_.city -State $_.State -Country $_.Country -DisplayName $_.DisplayName }\n<\/p>\n<p>The command and its associated output are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-4-14-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-4-14-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Removing a group of users<\/h2>\n<p>If I need to remove a group of users, I can once again use the CSV file. In fact, if I want to remove the users I just created, it is super simple. All I need to do is to use the <strong>Remove-MsolUser<\/strong> cmdlet instead of the <strong>New-MSOlUser<\/strong> cmdlet. I want also want to use the <strong>\u2013Force<\/strong> parameter. If I do not, the command will prompt for each user, which is probably not what I want to do. Here is the command without the <strong>\u2013Force<\/strong> parameter:<\/p>\n<p style=\"margin-left:30px\">\n  PS C:\\> $users = Import-Csv C:\\fso\\Office365Users.CSV\n<\/p>\n<p style=\"margin-left:30px\">\n  PS C:\\> $users | ForEach-Object { Remove-MsolUser -UserPrincipalName $_.userprincipalname}\n<\/p>\n<p style=\"margin-left:30px\">\n  Confirm\n<\/p>\n<p style=\"margin-left:30px\">\n  Continue with this operation?\n<\/p>\n<p style=\"margin-left:30px\">\n  [Y] Yes\u00a0 [N] No\u00a0 [S] Suspend\u00a0 [?] Help (default is &#8220;Y&#8221;):\n<\/p>\n<p>So instead, I use the <strong>\u2013Force<\/strong> parameter as shown here:<\/p>\n<p style=\"margin-left:30px\">\n  $users | ForEach-Object { Remove-MsolUser -UserPrincipalName $_.userprincipalname -Force}\n<\/p>\n<p>No output returns from this command.<\/p>\n<p>That is all there is to using the Azure\u00a0AD module to create a group of users. Office 365 Week will continue tomorrow when I will talk about more cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\" rel=\"noopener\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\" rel=\"noopener\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\" rel=\"noopener\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\" rel=\"noopener\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size:12px\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about creating bulk users in Office 365. Microsoft Scripting Guy, Ed Wilson, is here. This morning I spent a bit of time talking to my mentee. It is really cool, because yesterday I spent some time talking to my mentor. So it is sort of like the circle [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[359,291,3,45],"class_list":["post-870","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-office","tag-office-365","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about creating bulk users in Office 365. Microsoft Scripting Guy, Ed Wilson, is here. This morning I spent a bit of time talking to my mentee. It is really cool, because yesterday I spent some time talking to my mentor. So it is sort of like the circle [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=870"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/870\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}