Get and add users to securoty group

Dpa 26 Reputation points
2023-02-12T13:25:29.2533333+00:00

Trying add users with a specific details in the company field in AD to a security group and was not able to. Below is the script, any help?

#Retrieve a list of users with '123' in AD company field.

Get-aduser -Server (server name) -properties Company -Filter { enabled -eq $true -and company -like "Testing" } | select name, | Export-Csv C:\Temp\123.csv

$Users = Import-Csv "C:\Temp\123.csv"

foreach ($User in $Users) {

Add-ADGroupMember -Identity "123-Users" -Members $users }

Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 44,751 Reputation points
    2023-02-13T17:20:09.84+00:00
    Hello there,
    
    I would suggest checking the CSV files and the data.
    
    To specify the group and members we can use the following values:
    
    -distinguished name
    -objectGUID (GUID)
    -objectSid (security identifier)
    -SAMAccountName (Security Account Manager account name)
    
    You can import CSV file in PowerShell scripts and load users in a local variable as below
    
    #Import csv file and loads adusers in variable
    $Users = Import-Csv -Path "C:\adusers.csv"
    #Iterate AdUsers to add user account in Group
    foreach($User in $Users){
            try
            {
                Add-ADGroupMember -Identity Finance -Members $User.User -ErrorAction Stop -Verbose
            }
            catch
            {
                Write-Host "Error while adding user to adgroup"
            }
        }
    
    Hope this resolves your Query !!
    
    --If the reply is helpful, please Upvote and Accept it as an answer–
    
    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

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.