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

Welcome Forums General PowerShell Q&A 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

 
Participant
3 years ago.

  • Author
    Posts
  • #31942

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    Look at Import-CSV and ForEach-Object

The topic ‘Powershell read csv file and issue Active Directory command with row variable’ is closed to new replies.