Configure a DHCP Scope in Server 2022 Using PowerShell
As an essential part of a DHCP Server, we create and configure a DHCP Scope to specify the range of IP addresses from which a DHCP server can assign one to a client in our subnet. That is the primary and well-known use of a DHCP Scope. There are multiple ways to create and configure a DHCP Scope in Windows Server OSs including Windows Server 2022. One interesting, easy, and yet professional method is using Windows PowerShell. Throughout this article, we will cover the steps to configure a DHCP Scope in Windows Server 2022 Using PowerShell.
To create and configure a DHCP Scope using the GUI method, read Create And Configure A DHCP Scope In Windows Server 2022 article.
You may also like to read: Install And Configure DHCP In Windows Server 2022 Using PowerShell.
DHCP Scope Configuration Steps Using PowerShell
Using Windows PowerShell, we just write the commands and run them directly. That means, we must know the related commands and have a good knowledge of working with PowerShell. This section will describe all the details to configure a DHCP Scope in Server 2022 using PowerShell in the below simple steps. Let’s dive into it.
Step 1: Open Windows PowerShell
In the Start Menu right-click on Windows PowerShell and choose Run as Administrator. Search for it if it is not available in the Start Menu. We can also open it from the Run by typing powershell in the Run dialogue box.
Step 2: Create a New DHCP Scope Using PowerShell
To create a new scope, we use the Add-DhcpServerv4Scope PowerShell command. Using this command, we need to specify the following four parameters:
- The first one is the scope name. We use the -Name parameter name followed by the scope name enclosed in the double quotation.
- Secondly, we specify the start of the range IP address using the -StartRange parameter name followed by its value (IP address).
- The third one is to specify the end of the rage IP address using the -EndRange parameter name followed by its value (IP address).
- And, the last one is to specify the subnet mask IP address using -SubnetMask parameter name followed by its address.
As of my new scope, the whole command becomes Add-DhcpServerv4Scope -Name "Office B" -StartRange 192.168.1.100 -EndRange 192.168.1.2 -SubnetMask 255.255.255.0
See the picture illustration below.
Step 3: Verify DHCP Scope Using PowerShell
Now that we have created our new DHCP Scope in step 2, we can verify it using the Get-DhcpServerV4Scope
PowerShell command. Using this command, we can see a list of DHCP Scopes available on our DHCP Server. We can see the details for each of the scopes such as Scop ID which is most often used for further configuration of a DHCP Scope. See the illustration in the below image.
Step 4: Add Exclusion Range in a DHCP Scope Using PowerShell
To add a new Exclusion Range in a DHCP Scope, we use the Add-DhcpServerv4ExclusionRange PowerShell command. We specify the scope using the -ScopeId property. Then we use the -StartRange and -EndRange parameters each followed by the IP address that makes up the exclusion range. The command we used to create an exclusion range isAdd-DhcpServerv4ExclusionRange -ScopeId 192.168.1.0 -StartRange 192.168.1.150 -EndRange 192.168.1.160
. Looking into it, we see that we have created an exclusion range that starts at 192.168.1.150 and ends at 192.168.1.160 in our scope with scope ID 192.168.1.0.
We can also verify the Exclusion Range(s) in a specific DHCP Scope. To do so, use the command Get-DhcpServerv4ExclusionRange. We should use the Scope ID with this command to point to our specific Scope. So, our command is Get-DhcpServerv4ExclusionRange -ScopeId 192.168.1.0
. As we see, the picture below shows the exclusion range we just created.
Step 5: Set the Lease Duration Using PowerShell
Use the Set-DhcpServerv4Scope PowerShell command to set the lease duration. We must point to the scope using the -ScopeId parameter name followed by its value. Then we just specify the Lease Duration using the –LeaseDuration parameter name followed by its value. The format to define the Lease Duration value is day.hours:minutes:seconds. Saying those explanations, the whole command to define the Lease Duration, in our example, is Set-DhcpServerv4Scope -ScopeId 192.168.1.0 -LeaseDuration 10.00:00:00
. Also shown in the below image. A duration of 10 days is defined for the Lease Duration in our example.
Now, to verify the Lease Duration configuration, use Get-DhcpServerv4Scope
the command. By default, it is set to 8 days. But as we defined 10 days for it, we can see the changes.
Step 6: Configure the Default Gateway Using PowerShell
We can specify the Default Gateway for a DHCP Scope using the Set-DhcpServerv4OptionValue command. Like other steps, we need to point to the Scope using the Scope ID. We use the parameter name -Router followed by its value (IP address) to define the Default Gateway for a DHCP Scope. In our example, the complete command is Set-DhcpServerv4OptionValue -ScopeId 192.168.1.0 -Router 192.168.1.1
. We set 192.168.1.1 as the Default Gateway address for our DHCP Scope.
We will verify the Default Gateway configuration at the end of the next step, as we use the same command to verify these two steps.
Step 7: Configure DNS Domain and DNS Server Using PowerShell
As the Default Gateway configuration, we use the Set-DhcpServerv4OptionValue PowerShell command to configure the DNS Domain and DNS Server. We can configure the three of them in one command line using their parameter names and their values. Use -DnsServer parameter name followed by its value (IP Address) to configure the DNS Server. Use the -DnsDomain parameter name followed by its value enclosed in double quotation to configure the DNS Domain. Remember to address the scope using the Scope ID properties. In my example, the whole command becomes Set-DhcpServerv4OptionValue -ScopeId 192.168.1.0 -DnsServer 192.168.1.2 -DnsDomain "Technig.com"
.
To verify the Default Gateway configuration, DNS Server configuration, and DNS Domain configuration, use the Get-DhcpServerv4OptionValue PowerShell command followed by the Scope ID properties. In my case, Get-DhcpServerv4OptionValue -ScopeId 192.168.1.0
is the whole command line.
That is all with the DHCP Scope configuration using PowerShell commands. We can do all the settings and configuration, which we could do with the GUI method, using PowerShell. This method may not be user-friendly, but it is a time-saving, professional and maybe elegant method.
Delete A DHCP Scope Using PowerShell
In addition to the above DHCP configuration steps, this is the appropriate point to describe how we can delete a DHCP Scope using Windows PowerShell. To delete a DHCP Scope, use the -Remove-DhcpServerv4Scope command followed by the -ScopeId properties. Considering the DHCP scope I work with, the complete command is -Remove-DhcpServerv4Scope -ScopeId 192.168.1.10
. See the picture below.
Now, to verify if it is deleted, run the Get-DhcpServerV4Scope command and there will no longer be that scope available.
As simple as that, we can delete a DHCP Scope and verify the deletion using Windows PowerShell.
Conclusion
In a step by step approach, we covered how to configure a DHCP Scope in Windows Server 2022 using Windows PowerShell. We discussed the PowerShell command for creating a new DHCP Scope in an initial step. Then we went through the steps to add an exclusion range, the default gateway, DNS Domain, and DNS Server to a DHCP Scope all using the PowerShell command. Of course, we also verified each step. Lastly, we covered how we can remove a DHCP scope using PowerShell.
I hope you found this article helpful to your questions. You can always share us your thoughts and queries in the comment section.