{"id":4326,"date":"2013-01-14T00:01:00","date_gmt":"2013-01-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/01\/14\/use-powershell-to-create-ipv4-scopes-on-your-dhcp-server\/"},"modified":"2013-01-14T00:01:00","modified_gmt":"2013-01-14T00:01:00","slug":"use-powershell-to-create-ipv4-scopes-on-your-dhcp-server","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-ipv4-scopes-on-your-dhcp-server\/","title":{"rendered":"Use PowerShell to Create IPv4 Scopes on Your DHCP Server"},"content":{"rendered":"<p><strong>Summary:<\/strong> Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create IPv4 scopes on a Windows Server 2012 DHCP server.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. As I expected, it is cold, wet, and rainy in Redmond this week. Oh well, the cool thing is getting together with my teammates. The neat thing about Microsoft is that everyone is smart. The bad thing is that, working remotely, I do not get the benefit of spending time with them. So when we have meetings, it is always a great time for breakfast meetings, lunch meetings, dinner meetings, and even evening conversations. Luckily, I brought a nice assortment of tea with me, and believe me, this is great tea weather.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;This is the fifth 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\">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\">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\">Weekend Scripter: DHCP Address Conflict Detection<\/a><em> <\/em>in which I talked about making changes to the DHCP server configuration. Yesterday, 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><span>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<\/span>.<\/p>\n<h2>Use PowerShell to create DHCP scopes<\/h2>\n<p>If I only need to create a single DHCP scope, I will normally use the DHCP tool. After all, I have the Remote Server Admin Tools (RSAT) installed on my laptop running Windows&nbsp;8, and I even have a custom MMC that contains all of the normal tools I use on a regular basis.<\/p>\n<p>However, if I have more than one DHCP scope to create, I use the <strong>Add-DhcpServerv4Scope<\/strong> function to create the scope. In fact, the function is so easy to use, and so fast, that if Windows PowerShell is already open (which on my computer is a given), I can click on the shortcut for my management MMC, switch to Windows PowerShell, and create the DHCP scope before the MMC fully loads and presents the GUI.<\/p>\n<p>In the command that follows, I create a new DHCP scope named <em>bogus scope. <\/em>I assign an address range of 192.168.5.1 &ndash; 192.168.5.100. I use a \/24 subnet mask of 255.255.255.0. I also specify the description as &lsquo;created for HSG article&rsquo;. The name of the DHCP server is WDS1. Here is the command I created to accomplish this task.<\/p>\n<p style=\"padding-left: 30px\">Add-DhcpServerv4Scope -Name &#8216;bogus scope&#8217; -StartRange 192.168.5.1 -EndRange 192.168.5.100 -SubnetMask 255.255.255.0 -Description &#8216;created for HSG article&#8217; &ndash;cn wds1<\/p>\n<p>Well, the MMC has finally made an appearance, so I switch to that to verify the scope is correct. The image that follows illustrates this.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4810.HSG-1-14-13-01.png\"><img decoding=\"async\" title=\"Image of DHCP tool\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4810.HSG-1-14-13-01.png\" alt=\"Image of DHCP tool\" \/><\/a><\/p>\n<p>Interestingly enough, I consider it a best practice with DHCP to add the description to the scope. Unfortunately, the default view of the DHCP tool does not display the description property. Instead, I have to right-click the scope, select <strong>Properties<\/strong>, and on the <strong>General<\/strong> tab on the bottom, I can find the description of the scope. But there is no way, that I know of, to see all scopes, their names, and their descriptions at the same time. This, however, is really easy to do by using Windows PowerShell. I simply select the scopeid, name, and description from the values returned by the <strong>Get-DhcpServerv4Scope<\/strong> function. This command and its associated output are shown here.<\/p>\n<p style=\"padding-left: 30px\">16:59 C:\\&gt; Get-DhcpServerv4Scope -cn wds1 | select scopeid, name, description<\/p>\n<p style=\"padding-left: 30px\">ScopeId&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; description<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">192.168.0.0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new lan<\/p>\n<p style=\"padding-left: 30px\">192.168.2.0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WireLess&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Scope for WireLess Devices<\/p>\n<p style=\"padding-left: 30px\">192.168.3.0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Autodeploy&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Scope for WDS<\/p>\n<p style=\"padding-left: 30px\">192.168.5.0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bogus scope&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; created for HSG article<\/p>\n<h2>Delete a DHCP scope<\/h2>\n<p>Deleting a DHCP scope is dangerously easy. All I need to do is to run the <strong>Remove-DhcpServerv4Scope<\/strong> function and specify the scope ID. No force, no confirm, no whatever. If I have the rights to do it, Windows PowerShell obliges. Additionally, nothing returns from running the function, as shown here.<\/p>\n<p style=\"padding-left: 30px\">Remove-DhcpServerv4Scope -ScopeId 192.168.5.0 -cn wds1<\/p>\n<p>I use the <strong>Get-DhcpServerv4Scope<\/strong> function to verify the command works as desired. The following illustrates both the command and the output from the command.<\/p>\n<p style=\"padding-left: 30px\">17:01 C:\\&gt; Get-DhcpServerv4Scope -cn wds1<\/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.0.0&nbsp;&nbsp;&nbsp;&nbsp; 255.255.255.0&nbsp;&nbsp; lan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Active&nbsp;&nbsp; 192.168.0.40&nbsp;&nbsp;&nbsp; 192.168.0.90<\/p>\n<p style=\"padding-left: 30px\">192.168.2.0&nbsp;&nbsp;&nbsp;&nbsp; 255.255.255.0&nbsp;&nbsp; WireLess&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Active&nbsp;&nbsp; 192.168.2.50&nbsp;&nbsp;&nbsp; 192.168.2.99<\/p>\n<p style=\"padding-left: 30px\">192.168.3.0&nbsp;&nbsp;&nbsp;&nbsp; 255.255.255.0&nbsp;&nbsp; Autodeploy&nbsp;&nbsp;&nbsp;&nbsp; Active&nbsp;&nbsp; 192.168.3.1&nbsp;&nbsp;&nbsp;&nbsp; 192.168.3&#8230;<\/p>\n<h2>Best practice when deleting scopes? Use &ndash;whatif first!<\/h2>\n<p>Ok, so it is really easy to delete DHCP scopes in Windows PowerShell. On a production server, I do not want to get careless and hose my infrastructure. So, as a best practice, I use the <strong>&ndash;whatif<\/strong> parameter first to ensure that the command does exactly what I think it will do. In the following commands, I re-create my DHCP scope, and then use the <strong>&ndash;whatif<\/strong> parameter to verify that the command removes the appropriate scope.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Add-DhcpServerv4Scope -Name &#8216;bogus scope&#8217; -StartRange 192.168.5.1 -EndRange 192.168.5.100 -SubnetMask 255.255.255.0 -Description &#8216;created for HSG article&#8217; &ndash;cn wds1<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Remove-DhcpServerv4Scope -ScopeId 192.168.5.0 -cn wds1 -WhatIf<\/p>\n<p style=\"padding-left: 30px\">What if: The scope 192.168.5.0 will be removed from server wds1. This scope is active. &nbsp;Any leases present in the scope will also be deleted.<\/p>\n<p>After I verify that the command does what I want it to do, I use the Up Arrow to retrieve the command, and I remove the <strong>&ndash;whatif<\/strong> switch, as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Remove-DhcpServerv4Scope -ScopeId 192.168.5.0 -cn wds1<\/p>\n<p>Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/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, talks about using Windows PowerShell to create IPv4 scopes on a Windows Server 2012 DHCP server. Microsoft Scripting Guy, Ed Wilson, is here. As I expected, it is cold, wet, and rainy in Redmond this week. Oh well, the cool thing is getting together with my teammates. The neat [&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-4326","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, talks about using Windows PowerShell to create IPv4 scopes on a Windows Server 2012 DHCP server. Microsoft Scripting Guy, Ed Wilson, is here. As I expected, it is cold, wet, and rainy in Redmond this week. Oh well, the cool thing is getting together with my teammates. The neat [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4326","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=4326"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4326\/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=4326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}