Skip to content
The Chat Archive
claude-instant-1· Anthropic Arena Human Preference 55K

Give me a powershell script that reads a csv with titles and the corresponding group…

  1. User

    Give me a powershell script that reads a csv with titles and the corresponding group memberships needed for each title. It should then assign users membership based on their title in active directory.

  2. Assistant

    Here is a PowerShell script that will read a CSV with titles and group memberships, and assign users group memberships based on their title in Active Directory:

    # Import the CSV with titles and groups
    $TitlesGroups = Import-Csv -Path "C:\TitlesGroups.csv" 
    
    # Loop through each row in the CSV
    Foreach ($Row in $TitlesGroups) {
    
      # Get the title and groups from the current row
      $Title = $Row.Title
      $Groups = $Row.Groups
    
      # Filter AD users by title
      $Users = Get-ADUser -Filter "title -eq '$Title'"
    
      # Loop through each user returned
      Foreach ($User in $Users) {
    
        # Loop through each group
        Foreach ($Group in $Groups.Split(",")) {
    
          # Trim whitespace from group name
          $Group = $Group.Trim()  
    
          # Add user to group
          Add-ADGroupMember -Identity $Group -Members $User.DistinguishedName
    
        }
    
      }
    
    }
    

    The CSV should have two columns - Title and Groups. Groups should be a comma separated list of group names. This will lookup users by their title property in Active Directory, and add them to the appropriate groups based on the mapping in the CSV.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.

Identical first prompt