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 Profile photo of Curtis Smith Curtis Smith 11 months, 2 weeks ago.

  • Author
  • #31942
    Profile photo of Gary Seven
    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
    Profile photo of Curtis Smith
    Curtis Smith

    Look at Import-CSV and ForEach-Object

You must be logged in to reply to this topic.