powershell script to disable and move computers to other ou

corne nietnodig 196 Reputation points
2021-01-06T09:01:46.277+00:00

I want to disable a bunch of computers and move them to another OU in AD thru Powershell. Cannot get to work though, have tried several scripts but it does not work: cannot find object beneath dc=xxx,dc=xxx

However when i do a move adcomputer in powershell directly it does work, so there is something wrong with the get-content when i want to get the input from a file, can someone tell me what is wrong in the script?

Disable computeraccounts via een txt file

$Pclist = Get-Content C:\scripts\Computers.txt # Specify the path to the computers list.
Foreach($pc in $Pclist)
{
Disable-ADAccount -Identity "$pc."
Get-ADComputer -Identity "$pc" | Move-ADObject -TargetPath "OU=1,OU=2,OU=3,DC=domain,DC=name"
}

In computers.txt there are the computers:

computer1

computer2

computer3

When i do this it works:

get-adcomputer computername | Move-ADObject -TargetPath "OU=1,OU=2,OU=3,DC=domain,DC=name"

This is the error when launching the script:

Disable-ADAccount : Kan geen object met id computer1 vinden onder DC=domain,DC=name.
At line:4 char:1

  • Disable-ADAccount -Identity "$pc"
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : ObjectNotFound: (computer1 :ADAccount) [Disable-ADAccount], ADIdentityNotFoundException
  • FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.DisableADAccount

Get-ADComputer : Kan geen object met id computer1 vinden onder DC=domain,DC=name.
At line:5 char:1

  • Get-ADComputer -Identity "$pc" | Move-ADObject -TargetPath "OU=1OU ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : ObjectNotFound: (computer1 :ADComputer) [Get-ADComputer], ADIdentityNotFoundException
  • FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,549 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue 37,541 Reputation points Microsoft Vendor
    2021-01-07T08:19:44.643+00:00

    Hi,

    See if this works for you. Tested in Windows Server 2016.

    $pclist = Get-Content C:\scripts\Computers.txt   
    $targetOU = "OU=1,OU=2,OU=3,DC=domain,DC=name"  
    foreach($pc in $pclist){  
        if ($pc.Trim()) {  
            Get-ADComputer $pc.Trim() | Disable-ADAccount -PassThru | Move-ADObject -TargetPath $targetOU  
        }  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.
    0 comments No comments

19 additional answers

Sort by: Most helpful
  1. Mohd Arif 51 Reputation points
    2021-01-06T09:28:47.16+00:00

    I think you do not even need to create loop. you could use simple command to achieve this task. I did not test it, but I think it should work

    $Pclist = Get-Content C:\scripts\Computers.txt

    $Pclist | Get-ADComputer | Set-ADComputer -Enabled $false

    $Pclist | Get-ADComputer | Move-ADObject -TargetPath "OU=1,OU=2,OU=3,DC=domain,DC=name"

    1 person found this answer helpful.
    0 comments No comments

  2. corne nietnodig 196 Reputation points
    2021-01-06T13:04:45.573+00:00

    Unfortunally when i use your script it also gives the same errormessages..

    0 comments No comments

  3. Andreas Baumgarten 111.1K Reputation points MVP
    2021-01-06T14:02:39.34+00:00

    Maybe this is helpful to find the computer objects in AD (not tested by myself):

    $Pclist = Get-Content C:\scripts\Computers.txt
    $Pclist | Get-ADComputer -SearchScope Subtree | Disable-ADAccount
    $Pclist | Get-ADComputer -SearchScope Subtree | Move-ADObject -TargetPath "OU=1,OU=2,OU=3,DC=domain,DC=name"
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  4. corne nietnodig 196 Reputation points
    2021-01-06T14:28:56.327+00:00

    Andreas,

    When i do that, run that script with the right OU name then it givves me a question to give a fillter:

    What am i supposed to fill in there? it says:
    cmdlet Get-ADComputer at command pipeline position 1
    Supply values for the following parameters:
    (Type !? for Help.)
    Filter:

    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.