Powershell read csv file and issue Active Directory command with row variable

This topic contains 1 reply, has 2 voices, and was last updated by  Curtis Smith 2 years, 8 months ago.

  • Author
  • #31942

    Gary Seven

    I need my powershell script to read a CSV file that has the domain name and Active Directory group. (memberof)
    The csv file would have these rows maybe up to 100 domain names of companies:
    Domain Group
    acme , traders
    boeing. , engineers

    I want the powershell script to read each line and issue the command based on this line
    Get-ADUser -filter "mail -like '*@acme*'" | ForEach-Object {Add-ADGroupMember -Identity 'traders' -Members $_ }
    Get-ADUser -filter "mail -like '*@boeing*'" | ForEach-Object {Add-ADGroupMember -Identity 'engineers' -Members $_ }
    So i need the script to loop through each line in CSV and insert the variable in the line like a DOS bat script variable &

    Get-ADUser -filter "mail -like '*&Domain*'" | ForEach-Object {Add-ADGroupMember -Identity '&Group' -Members $_ }
    Any example?

  • #31956

    Curtis Smith

    Look at Import-CSV and ForEach-Object

You must be logged in to reply to this topic.