You are currently viewing How to move Active Directory Computers to OUs based on IP addresses

How to move Active Directory Computers to OUs based on IP addresses

The below scripts can be used to move computer objects based on their IP address. It could be further used with redircmp which can be utilized to specify the default OU that computers objects are created into once they join the domain.

redircmp "OU=New Computers,DC=domain,DC=com"

Import-Module activedirectory
$PC=Get-ADcomputer -Filter * -Properties IPV4Address -SearchBase "OU=New Computers,dc=domain,dc=com"
foreach ($Computer in $WS)
	{
{Get-Adcomputer $Computer.Name | Move-ADObject -Targetpath "OU=New Computers,OU=Erbil,dc=domain,dc=com"}
}

		switch -wildcard ($Computer.IPV4Address)
			{
"192.168.10.*" {Get-Adcomputer $Computer.Name | Move-ADObject -Targetpath "OU=Group 1,dc=domain,dc=com"}
"192.168.20.*" {Get-Adcomputer $Computer.Name | Move-ADObject -Targetpath "OU=Group 2,dc=domain,dc=com"}
"192.168.30.*" {Get-Adcomputer $Computer.Name | Move-ADObject -Targetpath "OU=Group 3,dc=domain,dc=com"}
"192.168.40.*" {Get-Adcomputer $Computer.Name | Move-ADObject -Targetpath "OU=Group 4,OU=ParentGroup,dc=domain,dc=com"}
default {Get-Adcomputer $Computer.Name | Move-ADObject -Targetpath "OU=Unspecified Computers,dc=domain,dc=com"}
			}
	}

As per the last line of the script, any unmatched computer objects can be sent to a specific OU

The script can be scheduled to run as needed and it can be also be included with SCCM task sequences to run whenever a new device is provisioned

Leave a Reply