import user from a csv file in AD group RRS feed

  • Question

  • hello everyone I'm trying to import users from a csv file into a AD group  with this script but I always and continuously receive errors.
    the first part works normally but the second part where I have to import from a specific column I always have errors of cannot validate argument or parameter 'Membres'
    do you have any useful suggestions?
    below the script, thanks

    $gruppo = Read-host -Prompt "group from extract user"


    #$esporta = Read-Host -Prompt "Molto bene, ora indicami il percorso sul quale vuoi salvare il file csv generato (es. c:\temp\NomeFile.csv)"

    #$filename = $gruppo

    $esporta = "Q:\DOCs_Jack\UTENTI\Powershell_Script\$gruppo.csv"


    Get-ADGroupMember -Identity $gruppo | Get-ADUser -Properties Mail, DisplayName, LastLogonDate | Select-Object @{Label = "First Name";Expression = {$_.GivenName}}, 

                      @{Label = "Last Name";Expression = {$_.Surname}},

                      @{Label = "Display Name";Expression = {$_.DisplayName}},

                      @{Label = "Logon_Name";Expression = {$_.sAMAccountName}},

                      #@{Label = "Full address";Expression = {$_.StreetAddress}},

                      #@{Label = "City";Expression = {$_.City}},

                      #@{Label = "State";Expression = {$_.st}},

                      #@{Label = "Post Code";Expression = {$_.PostalCode}},

                      #@{Label = "Country/Region";Expression = {if (($_.Country -eq 'IT')  ) {''} Else {''}}},

                      #@{Label = "Job Title";Expression = {$_.Title}},

                      #@{Label = "Account Status";Expression = {if (($_.Enabled -eq 'TRUE')  ) {'Enabled'} Else {'Disabled'}}},

                     # @{Label = "Company";Expression = {$_.Company}},

                     # @{Label = "Description";Expression = {$_.Description}},

                     # @{Label = "Department";Expression = {$_.Department}},

                     # @{Label = "Script_accesso";Expression = {$_.scriptPath}},

                      @{Label = "OU";Expression = {$_.distinguishedName}},

                      @{Label = "Email";Expression = {$_.Mail}},

                      #@{Label = "Manager";Expression = {%{(Get-AdUser $_.Manager -server $ADServer -Properties DisplayName).DisplayName}}},

                      #@{Label = "Manager";Expression = {%{(Get-AdUser $_.Manager -server $ADServer -Properties DisplayName).DisplayName}}},                  

                      @{Label = "Last LogOn Date";Expression = {$_.lastlogondate}} | 


                      Export-Csv $esporta -NoTypeInformation -Delimiter ";"


    $msg = 'want import user in another group? [Y/N}'

    #$Logon_Name = $user.SamAccountName

    $Logon_Name = $_.sAMAccountName   

        do {

                $response = Read-Host -Prompt $msg

                if ($response -eq 'y')            


                $gruppo1= Read-Host -Prompt "Name of group want import user"

                $a= Import-Csv $esporta | % {$_.'Logon_Name'}

                Add-ADGroupMember -Identity $gruppo1 -Members $a #(Add-ADPrincipalGroupMembership -Identity $esporta -MemberOf $esporta) #Add-ADPrincipalGroupMembership -identity $Logon_name -MemberOf $gruppo1 #% {$_.'Logon Name'} | Add-ADGroupMember $gruppo1 -Member Samaccountname #% { Add-ADGroupMember $gruppo1 -Members SAMAccountName }


               } until ($strQuit -eq "N")     

    Friday, October 4, 2019 10:07 AM

All replies

  • You need to provide the delimiter while importing the CSV file as well.

    $a= Import-Csv $esporta -Delimiter ";"| % {$_.'Logon_Name'}

    • Proposed as answer by Hasan Reza Friday, October 4, 2019 12:43 PM
    Friday, October 4, 2019 10:42 AM
  • thank you very much I was going crazy and in the end as I imagined it was a simple thing are the first steps with powershell for me thanks again
    Friday, October 4, 2019 12:38 PM
  • Please do not post colorized code in this forum. Use the code posting tool provided.

    How to post code in Technet Forums


    Friday, October 4, 2019 2:27 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,


    Just do it.

    Monday, October 7, 2019 8:02 AM