{"id":4322,"date":"2013-01-15T00:01:00","date_gmt":"2013-01-15T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/01\/15\/use-powershell-to-create-multiple-dhcp-scopes-on-dhcp-servers\/"},"modified":"2013-01-15T00:01:00","modified_gmt":"2013-01-15T00:01:00","slug":"use-powershell-to-create-multiple-dhcp-scopes-on-dhcp-servers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-multiple-dhcp-scopes-on-dhcp-servers\/","title":{"rendered":"Use PowerShell to Create Multiple DHCP Scopes on DHCP Servers"},"content":{"rendered":"<p><strong>Summary:<\/strong> Microsoft Scripting Guy, Ed Wilson, shows how to use functions from the DHCPServer module to create multiple IPv4 scopes.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. One thing that is great about Windows PowerShell is that, in general, it always behaves in a similar fashion. This means that techniques I learned when working with Active Directory should also carry over when I am working with Microsoft Exchange Server. Of course, there are some idiosyncrasies, but in general, PowerShell is PowerShell is PowerShell. This is why it is important to learn the fundamentals of working with Windows PowerShell. Do not expect a Windows PowerShell book for every product known to Microsoft&mdash;because it will not happen. Learn Windows PowerShell, and you will be able to administer any product you need to use. Great places to start are the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/scripting+wife\/\" target=\"_blank\">Scripting Wife Blog posts<\/a>, as well as <a href=\"http:\/\/technet.microsoft.com\/en-us\/scriptcenter\/dd742419.aspx\" target=\"_blank\">the Learn PowerShell page<\/a> on the Script Center.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;This is the sixth article in a series of Hey, Scripting Guy! Blog posts where I talk about using Windows PowerShell to work with Microsoft DHCP servers. I began the series with <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/01\/10\/use-powershell-to-query-ad-ds-for-dhcp-servers.aspx\" target=\"_blank\">Use PowerShell to Query AD DS for DHCP Servers<\/a><em>, <\/em>in which I talked about using the <strong>Get-ADObject<\/strong> cmdlet to query for DHCP servers. I followed this with <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/01\/11\/use-a-powershell-functions-to-authorize-dhcp-servers.aspx\" target=\"_blank\">Use PowerShell Functions to Authorize DHCP Servers<\/a><em>. <\/em>In this blog post, I talked about using several Windows PowerShell functions from the DHCPServer module. I query for DHCP servers, authorize DHCP servers, and deauthorize DHCP servers in this post. Next, I wrote <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/01\/12\/weekend-scripter-dhcp-address-conflict-detection.aspx\" target=\"_blank\">Weekend Scripter: DHCP Address Conflict Detection<\/a> in which I talked about making changes to the DHCP server configuration. Then, I wrote <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/01\/13\/weekend-scripter-parsing-the-dhcp-database-no-way.aspx\" target=\"_blank\">Weekend Scripter: Parsing the DHCP Database? No Way!<\/a><em> <\/em>Yesterday, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/01\/14\/use-powershell-to-create-ipv4-scopes-on-your-dhcp-server.aspx\" target=\"_blank\">I wrote about creating a DHCP scope<\/a> via PowerShell. Today&rsquo;s post builds on these articles, and I recommend you read them in order as I am not level-setting in each article.<\/p>\n<h2>Creating multiple DHCP server scopes without scripting<\/h2>\n<p>Normally, one would think that to create multiple DHCP server scopes would require a lot of scripting. It might be complicated, but the time saved, over manual configuration, would be worth it. That might have been true in the old days, but with Windows PowerShell that is not true. In fact, I used a one-line logical command to create a half dozen DHCP server scopes. It took me less than five minutes to do this, and it worked perfectly the first time.<\/p>\n<p>The trick, if it can be called a trick, is to create a text (CSV) file that contains the information to create each of the DHCP scopes. The important thing is that each of the column headings needs to be the same as the function parameter names. The image that follows illustrates the CSV file I created.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5661.HSG-1-15-13-01.png\"><img decoding=\"async\" title=\"Image of CSV file\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5661.HSG-1-15-13-01.png\" alt=\"Image of CSV file\" \/><\/a><\/p>\n<h2>First, import the CSV file<\/h2>\n<p>To read the CSV file, use the <strong>Import-CSV<\/strong> cmdlet. The nice thing about this cmdlet is that it creates property\/value pairs from the file thereby creating a series of objects. Windows PowerShell loves objects, so whenever the opportunity presents itself, I always like to use an object rather than attempting to parse strings. The command to import the CSV file is shown here.<\/p>\n<p style=\"padding-left: 30px\">import-csv C:\\fso\\DHCPSubnets.txt<\/p>\n<p>Luckily, most of the <strong>Add-DhcpServerv4Scope<\/strong> parameters accept piped input. I used the <strong>&ndash;full<\/strong><em> <\/em>switch with the <strong>Get-Help<\/strong> cmdlet to examine the parameters. For example, the Help for the <strong>&ndash;StartRange<\/strong> parameter shows that it accepts piped input by <strong>PropertyName<\/strong>.<\/p>\n<p style=\"padding-left: 30px\">-StartRange &lt;IPAddress&gt;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Specifies the starting IP address of the range within the subnet from which<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; IP addresses should be leased by the DHCP server service.<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Required?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; true<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Position?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Default value<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Accept pipeline input?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; true (ByPropertyName)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Accept wildcard characters?&nbsp; False<\/p>\n<p>One thing that is interesting is that the <strong>-ComputerName<\/strong> parameter does not accept piped input. Therefore, even though I added a <strong>CN<\/strong> (parameter alias) column heading, I ended up not using it.<\/p>\n<h2>Now pipe the objects to Add-DHCPServerv4Scope<\/h2>\n<p>After importing the CSV file, pipe the resultant objects to the <strong>Add-DHCPServerv4Scope<\/strong> function. Because the <strong>ComputerName<\/strong> parameter does not accept piped input, I have to hard code the <strong>ComputerName<\/strong> parameter, as shown here.<\/p>\n<p style=\"padding-left: 30px\">import-csv C:\\fso\\DHCPSubnets.txt | Add-DhcpServerv4Scope -cn wds1<\/p>\n<p>No output comes from running this command. I have two choices when it comes to verifying the command. The first is to use Windows PowerShell as indicated here.<\/p>\n<p style=\"padding-left: 30px\">18:18 C:\\&gt; Get-DhcpServerv4Scope -cn wds1 | ? description -match &#8216;test&#8217;<\/p>\n<p style=\"padding-left: 30px\">ScopeId&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SubnetMask&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; State&nbsp;&nbsp;&nbsp; StartRange&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EndRange<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">192.168.6.0&nbsp;&nbsp;&nbsp;&nbsp; 255.255.255.0&nbsp;&nbsp; scope1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Active&nbsp;&nbsp; 192.168.6.1&nbsp;&nbsp;&nbsp;&nbsp; 192.168.6&#8230;<\/p>\n<p style=\"padding-left: 30px\">192.168.7.0&nbsp;&nbsp;&nbsp;&nbsp; 255.255.255.0&nbsp;&nbsp; scope2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Active&nbsp;&nbsp; 192.168.7.1&nbsp;&nbsp;&nbsp;&nbsp; 192.168.7&#8230;<\/p>\n<p style=\"padding-left: 30px\">192.168.8.0&nbsp;&nbsp;&nbsp;&nbsp; 255.255.255.0&nbsp;&nbsp; scope3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Active&nbsp;&nbsp; 192.168.8.1&nbsp;&nbsp;&nbsp;&nbsp; 192.168.8&#8230;<\/p>\n<p style=\"padding-left: 30px\">192.168.9.0&nbsp;&nbsp;&nbsp;&nbsp; 255.255.255.0&nbsp;&nbsp; scope4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Active&nbsp;&nbsp; 192.168.9.1&nbsp;&nbsp;&nbsp;&nbsp; 192.168.9&#8230;<\/p>\n<p style=\"padding-left: 30px\">192.168.10.0&nbsp;&nbsp;&nbsp; 255.255.255.0&nbsp;&nbsp; scope5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Active&nbsp;&nbsp; 192.168.10.1&nbsp;&nbsp;&nbsp; 192.168.1&#8230;<\/p>\n<p style=\"padding-left: 30px\">192.168.11.0&nbsp;&nbsp;&nbsp; 255.255.255.0&nbsp;&nbsp; scope6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Active&nbsp;&nbsp; 192.168.11.1&nbsp;&nbsp;&nbsp; 192.168.1&#8230;<\/p>\n<p>Of course, the other thing I can do is use the DHCP tool. This is shown here.<\/p>\n<p>&nbsp;<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8787.HSG-1-15-13-02.png\"><img decoding=\"async\" title=\"Image of DHCP tool\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8787.HSG-1-15-13-02.png\" alt=\"Image of DHCP tool\" \/><\/a><\/p>\n<h2>Removing the &lsquo;test&rsquo; DHCP scopes<\/h2>\n<p>One thing, from a test perspective, is that when I use Windows PowerShell to create something, I also want to know how to use Windows PowerShell to remove or delete the same something. This is important for people who do DEMOs and need to know how to reset their test environment. It is also important when running a complex script that needs running and tweaking for quite some time.<\/p>\n<p>Well, the good news is that to clean up after running the command for creating a bunch of DHCP scopes requires a very simple one-liner. Here it is.<\/p>\n<p style=\"padding-left: 30px\">Get-DhcpServerv4Scope -cn wds1 | ? description -match &#8216;test&#8217; | Remove-DhcpServerv4Scope -cn wds1<\/p>\n<p>Join me tomorrow when we have the fourth article in the Windows PowerShell Workflow series by Microsoft Windows PowerShell MVP Richard Siddaway. It is awesome, so don&rsquo;t miss out.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto: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.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use functions from the DHCPServer module to create multiple IPv4 scopes. Microsoft Scripting Guy, Ed Wilson, is here. One thing that is great about Windows PowerShell is that, in general, it always behaves in a similar fashion. This means that techniques I learned when working with [&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":[218,37,362,3,45,368],"class_list":["post-4322","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dhcp-server","tag-networking","tag-powershell-3","tag-scripting-guy","tag-windows-powershell","tag-windows-server-2012"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use functions from the DHCPServer module to create multiple IPv4 scopes. Microsoft Scripting Guy, Ed Wilson, is here. One thing that is great about Windows PowerShell is that, in general, it always behaves in a similar fashion. This means that techniques I learned when working with [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4322","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=4322"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4322\/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=4322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}