{"id":76,"date":"2013-10-18T22:12:33","date_gmt":"2013-10-18T22:12:33","guid":{"rendered":"http:\/\/www.edrockwell.com\/blog\/?p=76"},"modified":"2013-10-19T02:00:04","modified_gmt":"2013-10-19T02:00:04","slug":"how-to-import-ad-users-from-csv-file","status":"publish","type":"post","link":"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/","title":{"rendered":"How To: Import AD Users from .csv file"},"content":{"rendered":"<p>I had a project that required me to make over 40 domain accounts. I decided that it was time to create all the domain accounts with a Powershell script. The script I came up with uses an import csv file with all the accounts and info I needed in it. Make sure you take the time to plan a naming convention for your AD accounts. In this case, they were a type of service account for many different environments. To keep it quick, I decided to not auto-gen the passwords so I simply put them in the csv file and removed them when I was done. Well, it took me a day or so to figure out my script to create ad accounts because I had problems&#8230;<\/p>\n<p>My troubleshooting was a bit flawed but I didn&#8217;t know it until the very end (After running for all the users). I was having problems with the script ending in error <enter error here>. This is a very generic error. I knew that my accounts had spaces in them for $GivenName $Surname and $Name. So, I went through the trouble making sure that my variable properties with spaces had &#8220;&#8221; around them. Yes, that&#8217;s a pair of double quotes. My Display name I wanted to use had GivenName and Surname in it with a space. So it looks like this: Displayname = ($User.&#8221;GivenName&#8221;+&#8221; &#8220;+$User.&#8221;Surname&#8221;). The quotes around &#8220;GivenName&#8221; allowed me to use two names in the GivenName column of the .csv file and the same for &#8220;Surname&#8221;. This way I can create an account that looks like: First Second Third Forth. In other words, my &#8220;GivenName&#8221; in my CSV was First Second so I had to put &#8220;&#8221; around it in the script so it would read it as one word. What I missed is after everything was put together, the fields were over 20 characters. Well, the limit on Windows 2008 Account &#8220;Names&#8221; is 20 characters. Until I ran my script and found that it didn&#8217;t create about 1\/2 of them, I started analyzing the .csv file to figure out why it didn&#8217;t work. I found out that the ones that didn&#8217;t get created are the ones that were over 20 characters. <\/p>\n<p>Here is the script. You will notice that I&#8217;ve got a comment in the script for the .csv file&#8217;s header fields. You can add to them or remove as needed. I think it&#8217;s easier to view the powershell references on Microsoft&#8217;s site. Here is the link to Set-ADUser cmdlet: http:\/\/technet.microsoft.com\/en-us\/library\/ee617215.aspx. For each property, you need it in the script and in the .csv file. If there are special characters or spaces. Remember to use the &#8220;&#8221; around it in the script. Also, make sure you are not exceeding the field length in AD for each property. The sAMAccountName (pre-Windows 2000 logon name) is limited to 20 characters for user objects. This is what got me a few times \ud83d\ude41<\/p>\n<p>Let me know if you need anything below explained! I&#8217;ll answer all comments on this the same day if I can.<\/p>\n<pre lang=\"POWERSHELL\">\r\n# REQUIRE DA ACCOUNT  \r\nif (! ($ENV:USERNAME).ToUpper().EndsWith(\"ADM\"))\r\n{\r\n\tthrow \"SCRIPT MUST BE RUN WITH ADMIN ACCOUNT\"\r\n}\r\n\r\n# IMPORTING AD MODULE\r\nif (! @(get-module -name ActiveDirectory).count) \r\n{\r\n\timport-module ActiveDirectory\r\n}\r\n\r\n# GETTING USERS FROM CSV FILE\r\n\r\n### NOTE: The Account Column CAN NOT be more than 20 characters or it will fail on them!\r\n\r\n$Users = Import-CSV C:\\CreateADUsers.csv \r\n# columns are: GivenName,Surname,Name,Account,Password,Department,Description\r\n\r\n\r\n# CREATING USERS\r\n# If you don't have two word attributes, you can remove some of the \"\" below after the $User.\r\nforeach($User in $Users)\r\n{\r\n\t$Params = @{\r\n\t\tSamAccountName = $User.Account\r\n\t\tName = $User.\"Name\"\r\n\t\tGivenName = $User.\"GivenName\"\r\n\t\tSurname = $User.\"Surname\"\r\n\t\tDisplayname = ($User.\"GivenName\"+\" \"+$User.\"Surname\")\r\n\t\tUserPrincipalName = ($User.Account+\"@domain.com\")\r\n\t\tDepartment = $User.\"Department\"\r\n\t\tDescription = $User.\"Description\"\r\n\t\tPath = \"OU=Your OU,DC=domain,DC=com\"\r\n\t\tPasswordNeverExpires = $true\r\n\t\tAccountPassword = (ConvertTo-SecureString $User.Password -AsPlainText -Force)\r\n\t\tEnabled = $true\r\n\t}\r\n\tnew-ADUser @Params\r\n}\r\n<\/pre>\n<p>I&#8217;m a seasoned Systems Administrator with experience starting in the early 90&#8217;s when 286 computers with 20 and 30 Mhz processors running Windows 3.1 which was the newest operating system. <\/p>\n<p>&#8230;and that&#8217;s the way Ed does it \ud83d\ude42   &#8212; Thanks Scott J. for that \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had a project that required me to make over 40 domain accounts. I decided that it was time to create all the domain accounts with a Powershell script. The script I came up with uses an import csv file with all the accounts and info I needed in it. Make sure you take the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[39,40,21],"class_list":["post-76","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-add-aduser","tag-import-users-in-ad","tag-powershell-2"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How To: Import AD Users from .csv file - System Admin [RESOLVED]<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To: Import AD Users from .csv file - System Admin [RESOLVED]\" \/>\n<meta property=\"og:description\" content=\"I had a project that required me to make over 40 domain accounts. I decided that it was time to create all the domain accounts with a Powershell script. The script I came up with uses an import csv file with all the accounts and info I needed in it. Make sure you take the [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/\" \/>\n<meta property=\"og:site_name\" content=\"System Admin [RESOLVED]\" \/>\n<meta property=\"article:published_time\" content=\"2013-10-18T22:12:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-10-19T02:00:04+00:00\" \/>\n<meta name=\"author\" content=\"Ed Rockwell\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@EddieRock\" \/>\n<meta name=\"twitter:site\" content=\"@EddieRock\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ed Rockwell\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/\"},\"author\":{\"name\":\"Ed Rockwell\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7\"},\"headline\":\"How To: Import AD Users from .csv file\",\"datePublished\":\"2013-10-18T22:12:33+00:00\",\"dateModified\":\"2013-10-19T02:00:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/\"},\"wordCount\":562,\"commentCount\":0,\"keywords\":[\"Add-ADUser\",\"import users in ad\",\"Powershell\"],\"articleSection\":[\"PowerShell\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/\",\"url\":\"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/\",\"name\":\"How To: Import AD Users from .csv file - System Admin [RESOLVED]\",\"isPartOf\":{\"@id\":\"https:\/\/www.edrockwell.com\/blog\/#website\"},\"datePublished\":\"2013-10-18T22:12:33+00:00\",\"dateModified\":\"2013-10-19T02:00:04+00:00\",\"author\":{\"@id\":\"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.edrockwell.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How To: Import AD Users from .csv file\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/#website\",\"url\":\"https:\/\/www.edrockwell.com\/blog\/\",\"name\":\"System Admin [RESOLVED]\",\"description\":\"How To: Make My DevOps Life Easier\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.edrockwell.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7\",\"name\":\"Ed Rockwell\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g\",\"caption\":\"Ed Rockwell\"},\"sameAs\":[\"http:\/\/www.therockwells.net\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To: Import AD Users from .csv file - System Admin [RESOLVED]","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/","og_locale":"en_US","og_type":"article","og_title":"How To: Import AD Users from .csv file - System Admin [RESOLVED]","og_description":"I had a project that required me to make over 40 domain accounts. I decided that it was time to create all the domain accounts with a Powershell script. The script I came up with uses an import csv file with all the accounts and info I needed in it. Make sure you take the [&hellip;]","og_url":"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/","og_site_name":"System Admin [RESOLVED]","article_published_time":"2013-10-18T22:12:33+00:00","article_modified_time":"2013-10-19T02:00:04+00:00","author":"Ed Rockwell","twitter_card":"summary_large_image","twitter_creator":"@EddieRock","twitter_site":"@EddieRock","twitter_misc":{"Written by":"Ed Rockwell","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/#article","isPartOf":{"@id":"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/"},"author":{"name":"Ed Rockwell","@id":"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7"},"headline":"How To: Import AD Users from .csv file","datePublished":"2013-10-18T22:12:33+00:00","dateModified":"2013-10-19T02:00:04+00:00","mainEntityOfPage":{"@id":"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/"},"wordCount":562,"commentCount":0,"keywords":["Add-ADUser","import users in ad","Powershell"],"articleSection":["PowerShell"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/","url":"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/","name":"How To: Import AD Users from .csv file - System Admin [RESOLVED]","isPartOf":{"@id":"https:\/\/www.edrockwell.com\/blog\/#website"},"datePublished":"2013-10-18T22:12:33+00:00","dateModified":"2013-10-19T02:00:04+00:00","author":{"@id":"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7"},"breadcrumb":{"@id":"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.edrockwell.com\/blog\/how-to-import-ad-users-from-csv-file\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.edrockwell.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How To: Import AD Users from .csv file"}]},{"@type":"WebSite","@id":"https:\/\/www.edrockwell.com\/blog\/#website","url":"https:\/\/www.edrockwell.com\/blog\/","name":"System Admin [RESOLVED]","description":"How To: Make My DevOps Life Easier","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.edrockwell.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7","name":"Ed Rockwell","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g","caption":"Ed Rockwell"},"sameAs":["http:\/\/www.therockwells.net"]}]}},"_links":{"self":[{"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/posts\/76","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":7,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":84,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/posts\/76\/revisions\/84"}],"wp:attachment":[{"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}