Why does it takes long time for PowerShell script to take effect on AD

Pankajbhakta95 106 Reputation points
2021-04-28T02:27:09.007+00:00

Hi,

I am using the script below to read the names of users from a .csv file and disable users from AD.

The script works alright but most of the time it take 5 minutes or even more to take effect and disable the users.

Is this normal or there is a better way to run the script ?

I am running the script from PowerShell ISE on my laptop that is login to the domain.

Thanks

Pankaj


Import-Module ActiveDirectory

$userlist=Import-CSV C:\ADUSERS\DisableUsers.csv

ForEach ($user in $userlist)
{
Disable-ADAccount -Identity $($user.name)

 write-host "user $($user.name) has been disabled"

}


Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-04-29T21:56:54.55+00:00

    Hi @Pankajbhakta95 ,

    if the script runs fast and without any error ... but the AD User & Computer Console doesn't show the disabled users only after a delay of a few minutes it might be your script us using one DC and the AD User & Computer Console is connected to a different DC. This would explain why you see the result with a delay in the console -> The DCs have to replicate the changes first.

    You can try to set the DC in the script with this line:

    $PSDefaultParameterValues = @{"*-AD*:Server"="YOUR_DC-COMPUTERNAME"}  
    

    And you should select the same DC in the AD User & Computer console as well.

    ----------

    (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

5 additional answers

Sort by: Most helpful
  1. Pankajbhakta95 106 Reputation points
    2021-04-29T21:18:27.887+00:00

    Hi @Andreas Baumgarten ,
    I have just now tested the script running from my laptop.

    The script ran fine but it did not disable the users yet.

    I have not waited for 10 minutes before replying to you.

    So, both my script and your latest script runs fine without error but it takes very long time before it takes effect on the DC and disable the users.

    Unfortunately, I do not have a test environment.

    Since, the script is quite basic so I guess I shall have to make time with our colleagues and test it on live DC later.

    If you have any other suggestion kindly let me know.

    Thanks for your help.

    Pankaj

    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.