{"id":2640,"date":"2013-10-29T00:01:00","date_gmt":"2013-10-29T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/10\/29\/building-a-demo-active-directory-part-2\/"},"modified":"2013-10-29T00:01:00","modified_gmt":"2013-10-29T00:01:00","slug":"building-a-demo-active-directory-part-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/building-a-demo-active-directory-part-2\/","title":{"rendered":"Building a Demo Active Directory: Part 2"},"content":{"rendered":"<p><strong>Summary<\/strong>: Build Windows PowerShell variables to design an organizational unit structure.\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;Hey, Scripting Guy!\nCan Windows PowerShell provide a consistent way to build a demo structure in Active Directory that includes organizational units and security groups?\n&mdash;RL\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Hello RL,\nHonorary Scripting Guy, Sean Kearney here, filling in for our good friend, Ed.\nWelcome to Part 2 of this series. To catch up, read <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/10\/28\/building-a-demo-active-directory-part-1.aspx\" target=\"_blank\">Building a Demo Active Directory: Part 1<\/a>.\nWe absolutely can use Windows PowerShell in this way. A good friend of mine, Rick Claus from Microsoft, asked me this very same question. &ldquo;Hey, Sean,&rdquo; our good friend piped up after removing his Tilley. &ldquo;I have to build demo environments all the time, and I would like an easier way to build the structure. I don&rsquo;t suppose you know of a way to do this in Windows PowerShell, do you?&rdquo;\nOf course, the minute he said, &ldquo;PowerShell,&rdquo; the answer was, &ldquo;Yes, of course, O Green Hatted one, you can!&rdquo;\nSo our first challenge is to decide what our structure will look like. As a demo environment, we probably need a decent and simple structure&hellip;maybe a main unit, some offices, and a division in each office.<\/p>\n<p style=\"padding-left: 30px\">Contoso.local<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Offices<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Seattle<\/p>\n<p style=\"padding-left: 30px\">&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Accounting<\/p>\n<p style=\"padding-left: 30px\">&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HR<\/p>\n<p style=\"padding-left: 30px\">&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IT<\/p>\n<p style=\"padding-left: 30px\">&nbsp;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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IT<\/p>\n<p style=\"padding-left: 30px\">&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exec\nThen within each division, we might want a security group. For our demo environment, we can build a simple set of names, such as:<\/p>\n<p style=\"padding-left: 30px\">NewOrleans-Accounting<\/p>\n<p style=\"padding-left: 30px\">Ottawa-IT<\/p>\n<p style=\"padding-left: 30px\">Seattle&#8211;HR\nFirst we&rsquo;re going to name our Base OU. In this example, we&rsquo;re simply going to call it &ldquo;Offices&rdquo;, but you can choose any name you like. I&rsquo;d recommend something that actually makes sense. Calling your Base OU &ldquo;FlyingWombats&rdquo; might not make very much sense. But to each, his or her own&hellip;<\/p>\n<p style=\"padding-left: 30px\">$BaseOU=&rdquo;Offices&rdquo;\nNow we&rsquo;re going to name six cities that will sit as children under this OU:<\/p>\n<p style=\"padding-left: 30px\">$CityOU=&rdquo;Tokyo&rdquo;,&rdquo;Redmond&rdquo;,&rdquo;Ottawa&rdquo;,&rdquo;Madrid&rdquo;,&rdquo;New Orleans&rdquo;,&rdquo;Queensland&rdquo;\nNow we&rsquo;ll define the four divisions. Of course, it could be more, but we&rsquo;re going to keep this quiet simple:<\/p>\n<p style=\"padding-left: 30px\">$DivisionOU=&#8221;Sales&#8221;,&#8221;Marketing&#8221;,&#8221;HR&#8221;,&#8221;Finance&#8221;\nSo if you&rsquo;re thinking out loud, I&rsquo;ll bet you&rsquo;re guessing, &ldquo;Oh, this looks like an easy answer!&rdquo; Well, really it is!\nSo today we&rsquo;ll be using the cmdlet in Active Directory called <strong>New-ADOrganizationalUnit<\/strong>. This will allow us to populate the OU&rsquo;s in our Active Directory demo environment.\nTo work with these cmdlets properly, we need to supply the distinguished name for the path in many cases. So first let&rsquo;s define that. Our main root, of course, will be our domain name:<\/p>\n<p style=\"padding-left: 30px\">$Domain=&rdquo;DC=Contoso,DC=local&rdquo;\nNow we&rsquo;ll define the path for our company OU where all the offices will exist:<\/p>\n<p style=\"padding-left: 30px\">$CompanyPath=&#8221;OU=$BaseOU,&#8221;+$Domain\nNow that all the prep work is set up, what shall we do? Let&rsquo;s build a simple loop structure to build it up! First create the base organizational unit:<\/p>\n<p style=\"padding-left: 30px\">NEW-ADOrganizationalUnit -name $BaseOU -path $Domain\nNext we&rsquo;ll step through our list of cities and create some OU&rsquo;s based on them. Let&rsquo;s also remember that we&rsquo;ll be targeting the main <strong>CompanyPath<\/strong> as our starting point:<\/p>\n<p style=\"padding-left: 30px\"># Gather through list of Cities<\/p>\n<p style=\"padding-left: 30px\">Foreach ($City in $CityOU)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;# Create OU for City<\/p>\n<p style=\"padding-left: 30px\">&nbsp;NEW-ADOrganizationalUnit -path $CompanyPath -name $City\nSo far, so good? Next we&rsquo;ll step through our list of divisions and populate them. We need to adjust the path to incorporate each city as part of the path for the division:<\/p>\n<p style=\"padding-left: 30px\">&nbsp;# Gather through list of Divisions<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Foreach($Division in $DivisionOU)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Create Division within City<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NEW-ADOrganizationalUnit -path &#8220;OU=$City,$CompanyPath&#8221; -name $Division<\/p>\n<p style=\"padding-left: 30px\">&nbsp;}<\/p>\n<p style=\"padding-left: 30px\">}\nWith this loop completed, we now have a rudimentary structure that we can populate. But did I mention security groups? Why yes, I did. And that&rsquo;s for another story tomorrow.\nI invite you to follow the Scripting Guys 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 <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<strong>Sean Kearney<\/strong>, Honorary Scripting Guy and Windows PowerShell MVP&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Build Windows PowerShell variables to design an organizational unit structure. &nbsp;Hey, Scripting Guy! Can Windows PowerShell provide a consistent way to build a demo structure in Active Directory that includes organizational units and security groups? &mdash;RL &nbsp;Hello RL, Honorary Scripting Guy, Sean Kearney here, filling in for our good friend, Ed. Welcome to Part [&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,56,3,154,45],"class_list":["post-2640","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-guest-blogger","tag-scripting-guy","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Build Windows PowerShell variables to design an organizational unit structure. &nbsp;Hey, Scripting Guy! Can Windows PowerShell provide a consistent way to build a demo structure in Active Directory that includes organizational units and security groups? &mdash;RL &nbsp;Hello RL, Honorary Scripting Guy, Sean Kearney here, filling in for our good friend, Ed. Welcome to Part [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2640","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=2640"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2640\/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=2640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}