We help IT Professionals succeed at work.

Move computers from unassigned OU

Kelly Garcia
Kelly Garcia asked
Last Modified: 2017-03-31
Hi Guys,

I have written the script below and I only want to move machines upon meeting one of the conditions of the switch command, however the below moves every machine. please help!

$UnassignedComputers = Get-QADComputer -SearchRoot "CN=Computers,DC=X,DC=COM"
foreach ($UnassignedComputer in $UnassignedComputers){
$Prefix = $UnassignedComputer.NAME
$Adcomputer = Get-QADComputer -Name $Prefix
$Adcomputername = $Adcomputer | select -ExpandProperty name
switch -Wildcard ($Adcomputername) {
            "lp-lon*" {$ou="OU=London Laptops,OU=London Computers,OU=London,OU=Organisation,DC=X,DC=COM"}
            "pc-lon*" {$ou="OU=London PC,OU=London Computers,OU=London,OU=Organisation,DC=X,DC=COM"}
            "lp-rom*" {$ou="OU=Laptops,OU=Romania Computers,OU=Romania,OU=Organisation,DC=X,DC=COM"}
            "lp-sing*" {$ou="OU=Singapore Laptops,OU=Singapore,OU=Organisation,DC=X,DC=COM"}
	     if ($ou -ne $null) {Move-QADObject -Identity $AdComputer.DN -NewParentContainer $ou}


Open in new window

Watch Question

PowerShell Developer
Top Expert 2010
Unlock this solution and get a sample of our free trial.
(No credit card required)
Chris DentPowerShell Developer
Top Expert 2010

Just looking at the code again. You really don't need to do this:
    $Prefix = $UnassignedComputer.NAME
    $Adcomputer = Get-QADComputer -Name $Prefix
    $Adcomputername = $Adcomputer | select -ExpandProperty name

Open in new window

You already have the computer, it's in the $UnassignedComputer variable. If you change your variable name in the loop:
foreach ($adComputer in $UnassignedComputers) {

Open in new window

And make switch base itself on the Name property:
    $ou = switch -Wildcard ($adComputer.Name) {

Open in new window

You can simply delete those three extra, completely superfluous, lines without changing anything else.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.