In this script, we first define the name of the group we want to remove users from. Then, we use the Import-Csv cmdlet to read a CSV file that contains a list of user names in a column named “UserName”.

We then loop through each user in the CSV file and try to retrieve the corresponding user object using the Get-ADUser cmdlet. If the user object is found, we use the Remove-ADGroupMember cmdlet to remove the user from the group. We also use the -Confirm:$false parameter to suppress the confirmation prompt.

If the user object is not found, we simply write a message to the console indicating that the user was not found.

Make sure that the CSV file is properly formatted with the user names in a column named “UserName”. Also, make sure that you have the Active Directory module for PowerShell installed on your computer before running this script.

import-module Activedirectory
# Replace "Group Name" with the name of the group you want to remove users from
$group = "Group Name"

$users = Import-Csv -Path "C:\UsersToRemove.csv"

foreach ($user in $users) {
    $userName = $user.UserName
    $userObject = Get-ADUser -Identity $userName

    if ($userObject) {
        Remove-ADGroupMember -Identity $group -Members $userObject -Confirm:$false
        Write-Host "User '$userName' removed from group '$group'."
    }
    else {
        Write-Host "User '$userName' not found."
    }
}

By amit_g

Welcome to my IT Infra Blog! My name is Amit Kumar, and I am an IT infrastructure expert with over 11 years of experience in the field. Throughout my career, I have worked with a wide variety of systems and technologies, from network infrastructure and cloud computing to hardware and software development. On this blog, I aim to share my knowledge, insights, and opinions on all things related to IT infrastructure. From industry trends and best practices to tips and tricks for managing complex systems, my goal is to provide valuable information that will help IT professionals and enthusiasts alike. Whether you are a seasoned IT veteran or just getting started in the field, I hope you will find my blog to be a valuable resource. In addition to sharing my own thoughts and ideas, I also welcome feedback, comments, and questions from my readers. I believe that a collaborative approach is the best way to advance the field of IT infrastructure and I look forward to hearing from you. Thank you for visiting my blog, and I hope you will continue to follow along as I explore the fascinating world of IT infrastructure. Sincerely, Amit Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *