{"id":5031,"date":"2012-09-03T00:01:00","date_gmt":"2012-09-03T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/09\/03\/use-powershell-to-create-a-user-in-active-directory\/"},"modified":"2012-09-03T00:01:00","modified_gmt":"2012-09-03T00:01:00","slug":"use-powershell-to-create-a-user-in-active-directory","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-a-user-in-active-directory\/","title":{"rendered":"&#65279;Use PowerShell to Create a User in Active Directory"},"content":{"rendered":"<p><b>Summary<\/b>: Use the Active Directory cmdlet <b>New-ADUser<\/b> to create a new user via Windows PowerShell.\nMicrosoft Scripting Guy, Ed Wilson, is here. The Scripting Wife has the <a href=\"http:\/\/powershellsaturday.com\/002\/\" target=\"_blank\">Window PowerShell Saturday site<\/a> bookmarked on her Windows&nbsp;7 smart phone, and she keeps hitting refresh on the little Internet Explorer window to watch the available tickets for the Charlotte PowerShell Saturday event march down to zero. I will admit, the tickets are going rapidly. This is a great chance to learn Windows PowerShell, meet new friends, and have a lot of fun&mdash;all for the nominal cost of lunch.<\/p>\n<h2>Use splatting to simplify user creation<\/h2>\n<p>The <b>New-ADUser<\/b> Windows PowerShell cmdlet (from the ActiveDirectory module) has a ton of parameters. Filling these out makes for cumbersome code, and does little to encourage readability. In fact, I hate having to read the command syntax (via <b>Get-Help<\/b> or <b>Get-Command<\/b>) because it seems to go on and on and on! I figure that most (if not all) of the attributes that are available to supply for a user in Active Directory can be supplied directly via a cmdlet parameter. (To be honest, I have never had the patience to compare the user schema with the cmdlet parameters to see if something is missing. By the way, anything that is missing can be filled in via the <b>Set-ADUser<\/b> cmdlet.\nOne way to make things a bit easier to read (and to open up interesting automation possibilities) is to use the technique of splatting. I have posted <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=splatting&amp;sections=7618\" target=\"_blank\">a number of Hey, Scripting Guy! Blogs<\/a> that explain splatting or make use of the technique<\/p>\n<h2>Splatting uses a hash table<\/h2>\n<p>To use splatting, I create a hash table that has keys that are exactly the same as the names of the parameters of the cmdlet, and I store the hash table in a variable. I then assign values to the keys. These values are passed to the cmdlet when I pass the variable to the cmdlet that contains the hash table. The following command creates a hash table that contains a number of keys that are the same as the parameter names that are used by the <b>Get-ADUser<\/b> cmdlet.<\/p>\n<p style=\"padding-left: 30px\">$users = @{<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&#8220;name&#8221; = &#8220;fred&#8221;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&#8220;givenName&#8221; = &#8220;manfred&#8221;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&#8220;l&#8221; = &#8220;lexington&#8221;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">}\nWhen I run this bit of code, I see the hash table.\n<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; $users = @{<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&gt;&gt;&nbsp; &#8220;name&#8221; = &#8220;fred&#8221;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&gt;&gt;&nbsp; &#8220;givenName&#8221; = &#8220;manfred&#8221;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&gt;&gt;&nbsp; &#8220;l&#8221; = &#8220;lexington&#8221;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&gt;&gt; }<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&gt;&gt;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">PS C:&gt; $users<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Value<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lexington<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">givenName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; manfred<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fred\nTo use this, all I do is import the ActiveDirectory module, create the hash table, and then pass the hash table to the <b>Get-ADUser<\/b> cmdlet. This code is shown here.\n<\/p>\n<p style=\"padding-left: 30px\">Import-Module activedirectory<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">$users = @{<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&#8220;name&#8221; = &#8220;fred&#8221;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&#8220;givenName&#8221; = &#8220;manfred&#8221;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&#8220;l&#8221; = &#8220;lexington&#8221;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">New-ADUser @users\nThat is it. Now, all I need to do is to figure out a way to read a file of some sort, and automatically create the hash tables. Hmmmm&hellip;shouldn&rsquo;t be too difficult, should it? After all, it is Windows PowerShell.&nbsp;&nbsp;\nJoin me tomorrow for more cool Windows PowerShell stuff.\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>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use the Active Directory cmdlet New-ADUser to create a new user via Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife has the Window PowerShell Saturday site bookmarked on her Windows&nbsp;7 smart phone, and she keeps hitting refresh on the little Internet Explorer window to watch the available tickets for the [&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,3,170,198,45],"class_list":["post-5031","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-splatting","tag-users","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use the Active Directory cmdlet New-ADUser to create a new user via Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife has the Window PowerShell Saturday site bookmarked on her Windows&nbsp;7 smart phone, and she keeps hitting refresh on the little Internet Explorer window to watch the available tickets for the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5031","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=5031"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5031\/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=5031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=5031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=5031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}