Home > Net >  script for disable user in AD
script for disable user in AD

Time:02-09

I'm trying to write a script to disable user when I run this script it says user is disable but when I check user is not disabled. Can someone please help me with this thanks. I'm new to PowerShell.

Import-Module ActiveDirectory

$username = Read-Host -Prompt 'Enter the user name.  '
$user = "$username"

{
    Disable-ADAccount -Identity $user
    write-host "user $($user) has been disabled"
}

CodePudding user response:

I have tested in my environment

As suggested by @Mathias R. Jessen, the code inside the curly braces { } doesn’t execute the code as expected

The workaround is to remove the curly braces { }

The final script will be as below:

Import-Module ActiveDirectory

$username = Read-Host -Prompt 'Enter the user name.  '
$user = "$username"
Disable-ADAccount -Identity $user
write-host "user $($user) has been disabled"
  •  Tags:  
  • Related