Disable Account Powershell


  • Hello guys.

    I have a question, i want to make a script that disable an user and moves the OU, but i need it to prompt the question "what user you want to disable" and read it.

    That i can do, but i need that to be the "postalCode" parameter.

    So would be something like this

    $cod = Read-Host "Type user code"

    Disable-ADACcount where postalCode = $cod

    Move-ADObject OU=ZENSA.........

    Any idead how i can do this ?

    I need to do by postalCode or any other different field, because i need to put my company code on that....

    Friday, March 24, 2017 5:39 PM

All replies

  • a very bad idea, because what if the postalcode is being used by multiple users ? Also i recommend posting this in the powershell forums although this would be a very simple script

    Friday, March 24, 2017 5:40 PM
  • Nope... we dont use postalCode field here, so we would start using to put the user Company ID there.

    Friday, March 24, 2017 5:56 PM
  • I would suggest using either the employeeID or employeeNumber attributes, just to avoid confusion.

    Edit: The Disable-ADAccount cmdlet does not support a filter (or a Where). You need to retrieve either sAMAccountName or distinguishedName, using Get-ADUser. For example (not tested):

    $ID = Read-Host "Enter postalCode"
    $NTName = (Get-ADUser -Filter {postalCode -eq $ID}).sAMAccountName
    If ($NTName.Count -eq 1)
        Disable-ADAccount -Identity $NTName
    Else {"More than one user, or no user found"}
    I check that one and only one user is found, to avoid problems.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Friday, March 24, 2017 6:27 PM
  • Worked as a charm man!

    Thanks =D

    Monday, March 27, 2017 10:34 AM
  • Hi,
    Thank you for the feedback, if it works for you, we would appreciate you to mark them as answers, it will be greatly helpful to others who have the same question.
    Best regards,

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact

    Tuesday, March 28, 2017 3:08 AM