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,520 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue 36,751 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. Andreas Baumgarten 108.7K Reputation points MVP
    2021-01-06T14:43:40.94+00:00

    What happens if you try this?

    $Pclist = Get-Content C:\scripts\Computers.txt # Specify the path to the computers list.
    Foreach($pc in $Pclist)
        {
        Get-ADComputer -Identity "$pc" -SearchScope Subtree | Disable-ADAccount
        Get-ADComputer -Identity "$pc" -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

    0 comments No comments

  2. corne nietnodig 196 Reputation points
    2021-01-06T14:45:20.233+00:00

    Get-ADComputer : Parameter set cannot be resolved using the specified named parameters.
    At line:5 char:6

    • Get-ADComputer -Identity "$pc" -SearchScope Subtree | Move-ADObj ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidArgument: (:) [Get-ADComputer], ParameterBindingException
    • FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
    0 comments No comments

  3. Andreas Baumgarten 108.7K Reputation points MVP
    2021-01-06T15:13:53.33+00:00

    Here we go. This is tested and it's working here:

    $DeletedCompOU = "OU=DisabledComputers,DC=whatever,DC=local"
    
    $Pclist = Get-Content C:\scripts\Computers.txt # Specify the path to the computers list.
     Foreach($pc in $Pclist)
         {
         $CompDN = (Get-ADComputer -Filter 'Name -eq $pc' -SearchScope Subtree).DistinguishedName
         Disable-ADAccount $CompDN
         Move-ADObject $CompDN -TargetPath $DeletedCompOU
         }
    

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

    Regards
    Andreas Baumgarten

    0 comments No comments

  4. Mohd Arif 51 Reputation points
    2021-01-06T15:14:12.557+00:00

    Instead of setting a loop or variable, can you please try to run below command and see if it works.

    Get-Content C:\scripts\Computers.txt | Get-ADComputer | Set-ADComputer -Enabled $false

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


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.