# Create DHCP Scope with Variables?

**URL:** https://forums.powershell.org/t/create-dhcp-scope-with-variables/13812
**Category:** PowerShell Help
**Created:** [February 17, 2020, 10:08am UTC](https://forums.powershell.org/t/create-dhcp-scope-with-variables/13812 "2020-02-17T10:08:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jool73](https://avatars.discourse-cdn.com/v4/letter/j/4491bb/32.png) [@jool73](https://forums.powershell.org/u/jool73)
#### Post date: [February 17, 2020, 10:08am UTC](https://forums.powershell.org/t/create-dhcp-scope-with-variables/13812/1 "2020-02-17T10:08:39Z")

</div>

Hi!

I need to Create a lot of DHCP scopes on diffrent server, i have tried to create a script that asks for the 3 first octets of the subnet, but i can´t get it to work as i want, does anyone has any idea how i can do it

I have done it this way ( the scripts ask for a lot of more info, but i have skipped the working parts)

```
$Subnet = Read-host "Enter Subnet for Office (ex. 192.25.58)"

Add-DhcpServerv4Scope -Name '$Office' -StartRange '$Subnet.100' -EndRange '$Subnet.150 '-SubnetMask 255.255.255.0 -Description '$office'
Set-DhcpServerv4OptionValue -DnsServer 11.11.0.30,11.11.0.31 -DnsDomain "int.domain.local" -Router $Subnet.2
```

The Scope details are always the same , the only thing that is changing is the 3 first octets… (the 2nd and 3rd octets)

---

<div class="post-metadata">

### Author: ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)
#### Post date: [February 17, 2020, 10:37am UTC](https://forums.powershell.org/t/create-dhcp-scope-with-variables/13812/2 "2020-02-17T10:37:34Z")

</div>

Single and double qoutes work differently. Double qoutes are expanded and will resolve variables. Single qoutes are literal, what you type is what is used.

```
PS C:\Users\rasim> $test = 'foo'

PS C:\Users\rasim> 'Test value is $test'
Test value is $test

PS C:\Users\rasim> "Test value is $test"
Test value is foo
```

There are a couple of other small things such as the DNS servers is a string array, not a string, so the values are separated by commas. Additionally, use splatting to make it a bit easier to read:

```
$Office = Read-Host "What office?"
$Subnet = Read-host “Enter Subnet for Office (ex. 192.25.58)”

$addParams = @{
    Name = "$Office"
    StartRange = "$Subnet.100" 
    EndRange = "$Subnet.150"
    SubnetMask = "255.255.255.0"
    Description = "$Office"
}

Add-DhcpServerv4Scope @addParams

$setParams = @{
    DnsServer = "11.11.0.30","11.11.0.31" 
    DnsDomain = “int.domain.local” 
    Router = "$Subnet.2"
}

Set-DhcpServerv4OptionValue @setParams
```

---

<div class="post-metadata">

### Author: ![jool73](https://avatars.discourse-cdn.com/v4/letter/j/4491bb/32.png) [@jool73](https://forums.powershell.org/u/jool73)
#### Post date: [February 18, 2020, 1:05pm UTC](https://forums.powershell.org/t/create-dhcp-scope-with-variables/13812/3 "2020-02-18T13:05:14Z")

</div>

Thanks for your incredible help 🙂

It worked like a charm, i need to learn little more about this !

&nbsp;

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:30pm UTC](https://forums.powershell.org/t/create-dhcp-scope-with-variables/13812/4 "2024-05-16T20:30:27Z")

</div>


