{"id":6571,"date":"2015-04-19T00:01:00","date_gmt":"2015-04-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/04\/19\/weekend-scripter-use-powershell-to-create-users-in-active-directory\/"},"modified":"2019-02-18T10:29:50","modified_gmt":"2019-02-18T17:29:50","slug":"weekend-scripter-use-powershell-to-create-users-in-active-directory","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-powershell-to-create-users-in-active-directory\/","title":{"rendered":"Weekend Scripter: Use PowerShell to Create Users in Active Directory"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to read a CSV file and create users in Active Directory.<\/span>\nMicrosoft Scripting Guy, Ed Wilson, is here. Yesterday in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2015\/04\/18\/weekend-scripter-oh-no-oh-wait-powershell-for-the-win.aspx\" target=\"_blank\">Oh No! Oh Wait&#8230;PowerShell for the Win!<\/a>, I created a CSV file from a Word document that I had been given. Today, I take the next logical step and create the users.\nWhen I have a working CSV file, I can begin the process of writing a script that will create the users in Active Directory.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> The CSV file that I was given had more than 1,200 lines in it. Although I gave it a quick &ldquo;once over glance,&rdquo; I did not check every single line in the file. So, I fully expect that there will be some potential problems with the data.\nThe first thing I want to do is to import the Active Directory module. I do this because I know that I am going to be using a few cmdlets from that module, so I may as well install it. This is actually a bit faster.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> I am working on a domain-joined computer running Windows&nbsp;8.1. I have access to the Active Directory module because I installed the Remote Server Admin Tools (RSAT). For more information about RSAT, including how to obtain the correct version and install the tools, see <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/08\/25\/weekend-scripter-install-free-powershell-remote-server-admin-tools.aspx\" target=\"_blank\">Install Free PowerShell Remote Server Admin Tools<\/a>.\nThe next thing I do is check to see if the organizational unit (OU) I intend to install is present. If the OU (named DataImport) is not present, then I create the OU. When I create it, I do so without setting the ProtectedFromAccidentalDeletion flag. (That is because I want to be able to delete this OU without having to do anything special.) Here are the first couple of lines of code:<\/p>\n<p style=\"margin-left:30px\">Import-Module ActiveDirectory<\/p>\n<p style=\"margin-left:30px\">If(!(Get-ADObject -Filter &#8220;name -eq &#8216;DataImport'&#8221;))<\/p>\n<p style=\"margin-left:30px\">&nbsp;{<\/p>\n<p style=\"margin-left:30px\">&nbsp; New-ADOrganizationalUnit -Name dataImport -Description &#8220;for data import&#8221; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; -ProtectedFromAccidentalDeletion:$false}\nThe next thing I do is import my CSV file:<\/p>\n<p style=\"margin-left:30px\">$names = Import-Csv -Encoding Unicode -Path C:DataInNames_Out.CSV&nbsp;\nNow, I walk through the CSV file and create all of the parameters I will use when I create my users. The <b>DisplayName<\/b> property is composed of the first name, the middle name (if it exists), and the last name. I pick up these values from my CSV file. The column headers are <b>Fname<\/b> (for first name), <b>Mname<\/b> (for middle name) and <b>Lname<\/b> (for last name). This is shown here:<\/p>\n<p style=\"margin-left:30px\">foreach($name in $names)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;$Params = @{<\/p>\n<p style=\"margin-left:30px\">&nbsp; DisplayName = &#8220;$($name.Fname) $($name.Mname) $($name.Lname)&#8221;\nI then specify the <b>GivenName<\/b> property. The <b>GivenName<\/b> property corresponds to the <b>Fname<\/b> field in my CSV file. Here is that assignment:<\/p>\n<p style=\"margin-left:30px\">GivenName = $name.Fname&nbsp;\nI use the first letter from the middle name to assign to the <b>Initials<\/b> property. To do this, I index into the array of letters that make up the middle name and choose the first one (it begins counting at 0). This is shown here:<\/p>\n<p style=\"margin-left:30px\">Initials = $name.Mname[0]&nbsp;\nThe <b>Surname<\/b> property receives the <b>Lname<\/b> field from my CSV file. As shown here, this is a straightforward value assignment:<\/p>\n<p style=\"margin-left:30px\">Surname = $name.Lname\nWhen I ran the script the first time, I received 21 error messages. This is because some of the names were too long. So I decided to cheat a bit and select only the first couple of letters from the first name and combine it with the last name. Here is the code I use to do this:<\/p>\n<p style=\"margin-left:30px\">Name = &#8220;$($name.Fname.Substring(0,2)) $($name.Lname)&#8221;\nI used the same logic for the <b>SamAccountName<\/b> property. The code appears here:<\/p>\n<p style=\"margin-left:30px\">SamAccountName = &#8220;$($name.Fname.Substring(0,2)).$($name.Lname)&#8221;\nI then assign the path to the organizational unit I will use to hold my newly created user accounts:<\/p>\n<p style=\"margin-left:30px\">Path = &#8220;ou=DataImport,DC=Nwtraders,Dc=Com&#8221;<\/p>\n<p style=\"margin-left:30px\">&nbsp;}\nBecause I expect the script to run into a few issues (for example, I do not check for duplicate account names), I use <b>Try<\/b>\/<b>Catch<\/b>\/<b>Finally<\/b>. The first thing I do is set the Error Action Preference to <b>Stop<\/b>. This will catch non-terminating errors. Then I create the new users, and if an error occurs, I display the display name. Finally, I set the value of the <b>$ErrorActionPreference<\/b> variable to <b>SilentlyContinue<\/b>.\nThe reason for setting the error action to <b>SilentlyContinue<\/b>, is that I do not check for the length of first names when I use <b>SubString<\/b> to select only the first two letters. In my data, I discovered that some user names did not include a complete first name, but rather, only had a first initial. This was causing a few of my initial errors. So because I knew about the issue, I decided to hide the error messages.\nThe code is shown here:<\/p>\n<p style=\"margin-left:30px\">$ErrorActionPreference = &#8220;Stop&#8221;<\/p>\n<p style=\"margin-left:30px\">&nbsp;Try {New-ADUser @Params}<\/p>\n<p style=\"margin-left:30px\">&nbsp;Catch { &#8220;Error creating &#8230; &#8221; ;$params.DisplayName }<\/p>\n<p style=\"margin-left:30px\">&nbsp;Finally {$ErrorActionPreference = &#8220;silentlycontinue&#8221;}<\/p>\n<p style=\"margin-left:30px\">}\nHere is the complete script:<\/p>\n<p style=\"margin-left:30px\">Import-Module ActiveDirectory<\/p>\n<p style=\"margin-left:30px\">If(!(Get-ADObject -Filter &#8220;name -eq &#8216;DataImport'&#8221;))<\/p>\n<p style=\"margin-left:30px\">&nbsp;{<\/p>\n<p style=\"margin-left:30px\">&nbsp; New-ADOrganizationalUnit -Name dataImport -Description &#8220;for data import&#8221; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; -ProtectedFromAccidentalDeletion:$false}<\/p>\n<p style=\"margin-left:30px\">$names = Import-Csv -Encoding Unicode -Path C:DataInNames_Out.CSV<\/p>\n<p style=\"margin-left:30px\">foreach($name in $names)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;$Params = @{<\/p>\n<p style=\"margin-left:30px\">&nbsp; DisplayName = &#8220;$($name.Fname) $($name.Mname) $($name.Lname)&#8221;<\/p>\n<p style=\"margin-left:30px\">&nbsp; GivenName = $name.Fname<\/p>\n<p style=\"margin-left:30px\">&nbsp; Initials = $name.Mname[0]<\/p>\n<p style=\"margin-left:30px\">&nbsp; Surname = $name.Lname<\/p>\n<p style=\"margin-left:30px\">&nbsp; Name = &#8220;$($name.Fname.Substring(0,2)) $($name.Lname)&#8221;<\/p>\n<p style=\"margin-left:30px\">&nbsp; SamAccountName = &#8220;$($name.Fname.Substring(0,2)).$($name.Lname)&#8221;<\/p>\n<p style=\"margin-left:30px\">&nbsp; Path = &#8220;ou=DataImport,DC=Nwtraders,Dc=Com&#8221;<\/p>\n<p style=\"margin-left:30px\">&nbsp;}<\/p>\n<p style=\"margin-left:30px\">&nbsp;$ErrorActionPreference = &#8220;Stop&#8221;<\/p>\n<p style=\"margin-left:30px\">&nbsp;Try {New-ADUser @Params}<\/p>\n<p style=\"margin-left:30px\">&nbsp;Catch { &#8220;Error creating &#8230; &#8221; ;$params.DisplayName }<\/p>\n<p style=\"margin-left:30px\">&nbsp;Finally {$ErrorActionPreference = &#8220;silentlycontinue&#8221;}<\/p>\n<p style=\"margin-left:30px\">}\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to read a CSV file and create users in Active Directory. Microsoft Scripting Guy, Ed Wilson, is here. Yesterday in Oh No! Oh Wait&#8230;PowerShell for the Win!, I created a CSV file from a Word document that I had been given. Today, I take [&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":[7,531,3,4,21,61,45],"class_list":["post-6571","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-comma-and-other-deliminited-files","tag-scripting-guy","tag-scripting-techniques","tag-string-manipulation","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to read a CSV file and create users in Active Directory. Microsoft Scripting Guy, Ed Wilson, is here. Yesterday in Oh No! Oh Wait&#8230;PowerShell for the Win!, I created a CSV file from a Word document that I had been given. Today, I take [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6571","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=6571"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6571\/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=6571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=6571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=6571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}