Powershell: How to disable computers in AD from a list RRS feed

  • Question

  • Hello everyone, hope this is the right place to ask this.

    I've been trying to get powershell to draw information from a csv file, which contains a list of computer names, which are in out AD.

    The next part, which i'm having trouble with, is actually getting the list of computers disabled.

    I've tried countless methods, and it seems that I can't get any of them to work, so here is my last ditch effort.

    Have a pleasant day.

    • Moved by Bill_Stewart Thursday, January 25, 2018 9:55 PM This is not "teach me computer basics" forum
    Friday, October 13, 2017 11:53 AM

All replies

  • Post your script so we can see what you are trying to do.


    Friday, October 13, 2017 12:15 PM
  • Why not just import you CSV into a variable, then run a foreach loop to go through each computer name and run something like:

    disable-adaccount -identity $variablefromloop

    Or move the computer to a different OU:

    get-adcomputer -identity $variablefromloop | move-adobject -targetpath 'OU=disabled computers,DC=domain,DC=lan"

    Friday, October 13, 2017 12:58 PM
    1. $MoveList = Import-Csv -Path "C:\Script\DisablePC2.csv"
    3. $TargetOU = '[REDACTED]'
    5. $countPC    = ($movelist).count
    7. foreach ($Computer in $MoveList){    
    8.     Write-Host " Moving Computer Accounts..."
    9.     Get-ADComputer $Computer.CN | Move-ADObject -TargetPath $TargetOU
    10. }

    Monday, October 23, 2017 12:56 PM
  • What is the error?


    Monday, October 23, 2017 1:04 PM
  • Get-ADComputer : Cannot Validate argument on parameter 'Identity'. The argument is null or an element of the argument collection contains a null value.
    At line:6 char:20
    + Get-ADComputer $Computer.CN | Move-ADObject -TargetPath $TargetOU
    +CategoryInfo : InvalidData: (:) [Get-ADComputer], ParameterBindingValidationException
    +FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Mangement.Commands.GetADComputer

    Get-ADComputer : Cannot validate argument on parameter 'Identity'. The argument is null or an element of the argument collection contains a null value.
    At line:6 char:20
    + Get-ADComputer $Computer.CN | Move-ADObject -TargetPath $TargetOU
    + CategoryInfo : InvalidData: (:) [Get-ADComputer], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Commands.GetADComputer

    Tuesday, October 24, 2017 6:12 AM
  • You cannot have empty fields in your CSV.


    Tuesday, October 24, 2017 6:23 AM
  • This is what the CSV file looks like.


    I've tried with and without comma, neither works.

    Tuesday, October 24, 2017 6:40 AM
  • It has no headers.


    Tuesday, October 24, 2017 6:45 AM
  • So how would it look with a header?
    Tuesday, October 24, 2017 6:54 AM
  • Tuesday, October 24, 2017 7:02 AM
  • I've tried these different formats,




    But they don't seem to work, don't know if i'm doing it wrong.

    Tuesday, October 24, 2017 7:35 AM
  • Please read the link I posted.  It explains exactly how to create a CSV file.


    Tuesday, October 24, 2017 7:39 AM
  • I've looked at the link, but all those examples are for multiple parameters.

    I've tried to write my CSV file like some of the examples in the link, but still no dice.

    Tuesday, October 24, 2017 11:16 AM
  • Do you understand the word "header"?  Your file needs a header that names the columns. I do not see what is so difficult about that.

    You can also add headers on import.

    help import-csv -par header

    If your CSV has three column of date for first name, last name and age then the header would be "firstname,lastname,age" without the quotes.

    See: https://en.wikipedia.org/wiki/Comma-separated_values


    Tuesday, October 24, 2017 11:22 AM
  • From what I understand I do have a header, but it still doesn't work.
    If my CSV file looks like this is it correct or no?


    Otherwise I'm just incredibly stupid.

    Tuesday, October 24, 2017 11:56 AM
  • Yes but does your code match?


    Tuesday, October 24, 2017 1:34 PM
    1. $MoveList = Import-Csv -Path "C:\Script\DisablePC2.csv"
    3. $TargetOU = '[REDACTED]'
    5. foreach ($Computer in $MoveList){    
    6.     Get-ADComputer $Computer | Move-ADObject -TargetPath $TargetOU
    7. }

    I have no idea, if the code matches. I'm a complete novice with powershell...

    Wednesday, October 25, 2017 5:56 AM
  • Your code needs to reference the header column for it to work.


    Without this you are not referencing the column.


    Wednesday, October 25, 2017 8:43 AM