In my previous article, Understanding TCP/IP Subnets, I explained the basics of converting IP addresses and subnet masks to binary to understand what's going on. Here, we do it with PowerShell.
If you're just looking for an online subnet calculator, try ours: TCP/IP Subnet Calculator
The first thing we do is convert the IP address and subnet mask into binary, so for example, if we input a subnet mask of 255.255.255.0, the binary representation is:
11111111 11111111 1111111 00000000
We then count the number of 1's which gives us the number of bits (netbits) assigned to the network ID (this is the meaning of the subnet mask). In this case, 24 bits are the network ID, the remaining 8 are the host ID.
Next, we get the actual network ID from our IP address by taking the first 24 (or how ever many netbits) characters from our binary IP address, and the rest of the 32 bits set to zero. For example, if our IP address is 192.168.1.5, the binary representation is:
11000000 10101000 00000001 00000101
and so our network ID is:
11000000 10101000 00000001 00000000 (192.168.1.0)
The network ID is commonly displayed with the number of netbits at the end, e.g. 192.168.1.0/24
The remaining bits (8 in our example) are used for the host ID portion of the IP address. As shown above, a host ID of all 0's is the network ID. A host ID of all 1's is the broadcast address. The valid range of assignable addresses in a subnet is from all 0's + 1 to all 1's -1. In our example, that's 00000001 (1) to 11111110 (254).
Try some different subnet masks and see what happens. For example, if you change the subnet mask to 255.255.255.128 (25 netbits), you'll see the range of assignable addresses get cut in half, and if you change the subnet mask to 255.255.254.0, you'll see the range of assignable addresses double.
The script also does some basic validation of the IP address and subnet mask (making sure that they are 32 bits long and that the host ID is not all 0's nor all 1's). This is certainly not an exhaustive check, there are lots of reserved addresses that would be considered not valid.
- Backup DFS Namespaces Using PowerShell
- Translate Active Directory Name Formats Using PowerShell
- List Linux Users in Active Directory Using PowerShell
- Enable Trust for Delegation in Active Directory Using PowerShell
- TCP/IP Subnet Math with PowerShell - What AD Site is that Server in?
- List Sites and Subnets in Active Directory with PowerShell
- Find Disabled Users in Active Directory with PowerShell
- List Forest-wide Group Memberships with PowerShell
- Find Old Computer Accounts in AD with PowerShell
- List SPNs in Active Directory with PowerShell
- List Domain Controllers in Active Directory