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. corne nietnodig 196 Reputation points
    2021-01-07T07:54:36.787+00:00

    That command does nothing when i execute it in ISE or in core powershell it gives me the exact command back, but no results.


  2. corne nietnodig 196 Reputation points
    2021-01-07T10:18:50.387+00:00

    Ian Xue, this worked although it returns an error that it cannot find a computer, it would be more decent that, if there is a computer in the computers.txt that does not excist anymore that the script is running thru or maybe thats the case, but this works!

    Thanks all!


  3. David Cruze 0 Reputation points
    2023-04-25T14:23:33.39+00:00

    i have written this script to disable the Computer object even if there in any OU

    $csv = import-csv c:\temp\test.csv -Header computer
    $Failures = @()
    $Success = @()
    Foreach ($computer in $csv.computer) {
        try { 
            $Disabled = Set-ADComputer -Identity $(($computer -replace '\s.*')).trim()  -Enabled $false -PassThru
            if ($Disabled) {
                $Success += $computer
            }
        }
        catch {
            $Failures += $computer
        }
    }
    
    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.