Moving AD objects between OU

Belgian Malinois 421 Reputation points
2025-02-05T02:14:31.1266667+00:00

We are upgrading Windows 10 to 11. I am trying to automate moving workstation objects between two OU's, however the script does not catch anything.

Import Active Directory module

Import-Module ActiveDirectory

Define Source and Target OUs

$SourceOU = “OU=Win10,OU=Fake Workstations,DC=Fake,DC=Fake,DC=Fake"

$TargetOU = "OU=Win11,OU=Fake Workstations,DC=Fake,DC=Fake,DC=Fake"

Search for computers in the Source OU running Windows 11 Enterprise

$Computers = Get-ADComputer -SearchBase $SourceOU -Filter * -Property OperatingSystem |

             Where-Object { $_.OperatingSystem -like “Windows 11" }

Check if any computers are found

if ($Computers.Count -gt 0) {

    foreach ($Computer in $Computers) {

        # Move each computer to the Target OU

        Write-Host "Moving $($Computer.Name) to $TargetOU"

        Move-ADObject -Identity $Computer.DistinguishedName -TargetPath $TargetOU

    }

    Write-Host "Operation completed successfully!"

} else {

    Write-Host "No Windows 11 Enterprise computers found in $SourceOU."

}

Microsoft Configuration Manager
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,791 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. XinGuo-MSFT 20,881 Reputation points
    2025-02-05T07:07:28.4933333+00:00

    Hi,

    It looks like there might be a couple of issues with your script. Let's go through it step-by-step to identify potential problems:

    The filter for OperatingSystem should match exactly. If the OS name is not exactly "Windows 11", it won't catch it. You might want to use a wildcard.

    Here's a revised version of your script:

    # Import Active Directory module
    Import-Module ActiveDirectory
    
    # Define Source and Target OUs
    $SourceOU = "OU=Win10,OU=Fake Workstations,DC=Fake,DC=Fake,DC=Fake"
    $TargetOU = "OU=Win11,OU=Fake Workstations,DC=Fake,DC=Fake,DC=Fake"
    
    # Search for computers in the Source OU running Windows 11
    $Computers = Get-ADComputer -SearchBase $SourceOU -Filter * -Property OperatingSystem |
                 Where-Object { $_.OperatingSystem -like "Windows 11*" }
    
    # Check if any computers are found
    if ($Computers.Count -gt 0) {
        foreach ($Computer in $Computers) {
            # Move each computer to the Target OU
            Write-Host "Moving $($Computer.Name) to $TargetOU"
            Move-ADObject -Identity $Computer.DistinguishedName -TargetPath $TargetOU
        }
        Write-Host "Operation completed successfully!"
    } else {
        Write-Host "No Windows 11 computers found in $SourceOU."
    }
    
    

    Give this a try and see if it resolves the issue. If you encounter any further problems, feel free to ask!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.