none
Powershell script to delete users in a group! RRS feed

  • Question

  • Hi,

    We have been recently experiencing little replication problem on our DC because of a deletion of a group which had almost 30000 objects on it. I want to avoid this in the future by creating a script that deletes only a certain numbers of users within a certain group. I mean to create a script that prompts the user to write the name of the group and the number of the members that the user wants to delete from it?

    Any idea how to accomplish this!

    Thanks in advance!

    PS. I will show later my attempt to reach this goal!

    • Moved by Bill_Stewart Tuesday, November 7, 2017 9:27 PM This is not "teach me basics of computer logic" forum
    Friday, September 8, 2017 12:46 PM

Answers

  • You have to remember that the group may be smaller than your number.

    $max_i = if($members.Count -gt 8){8}else{$members.Count}


    \_(ツ)_/



    • Edited by jrv Friday, September 8, 2017 3:11 PM
    • Marked as answer by Nikart Monday, October 1, 2018 8:02 AM
    Friday, September 8, 2017 3:10 PM

All replies

  • This forum is not for scripting requests. Rather help on specific scripting issues.

    What do you have so far? What errors is it giving you?

    We won't just write a script for you. Why not just open Active Directory Users and Computers and shift click on a bunch of users that you want to remove?

    Friday, September 8, 2017 12:58 PM
  • Why did you jump on me so fast. I wrote that I will share my attempt on creating this script! So have little patient, no one asked to create it for me from the zero!!!
    Friday, September 8, 2017 1:10 PM
  • Why did you jump on me so fast. I wrote that I will share my attempt on creating this script! So have little patient, no one asked to create it for me from the zero!!!

    Then start by posting your script and a specific question.

    Please read this first: This forum is for scripting questions rather than script requests  
    Also find scripts here: http://gallery.technet.microsoft.com

    You can learn how to write a script here


    \_(ツ)_/

    Friday, September 8, 2017 1:22 PM
  • I didn't "jump" on you, or at least I didn't mean to. It's just you should have waited and created a post which included the script as well as any errors that your are experiencing. That way we could actually help you out specifically to the errors you are experiencing or to any errors you have made in your code.

    As for my other points, I think they still hold up.

    Friday, September 8, 2017 1:23 PM
  • This is the script Im using.. If I choose to delete a lower number than the overall number of users on the group it works without problem.. but when I choose to delete all of the users, it deletes all of them but not the last one and its saying that the problem stand on this $members[$i]?!

    Import-Module activedirectory
    $group = Read-Host -Prompt "Insert group name"
    write-host "----------------------------------------------------------------"
    Write-Host "Please wait, it will take some time to complete the operation..."
    write-host "----------------------------------------------------------------"

    $members = Get-ADGroupMember $group

    for ($i = 0; $i -le 8; $i++)

       Remove-ADGroupMember -Identity $group -member $members[$i] -ErrorAction SilentlyContinue -Confirm:$false 
    }

    Friday, September 8, 2017 2:51 PM
  • You have to remember that the group may be smaller than your number.

    $max_i = if($members.Count -gt 8){8}else{$members.Count}


    \_(ツ)_/



    • Edited by jrv Friday, September 8, 2017 3:11 PM
    • Marked as answer by Nikart Monday, October 1, 2018 8:02 AM
    Friday, September 8, 2017 3:10 PM