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!