Remove-AdGroupMember via Foreach Object Array

I accidentally added over 400 users to the wrong Security Group. The users SamAccountName is stored in one column (no headers) in a text file. 

$wrongadmembers = Get-content C:\wrongmembers.txt

Remove-AdGroupMember -Identity "ZG_East_DM" -members $wrongmembers but it obviously didnt work

Can anyone help me put together a ForEach-Object array that will allow me to remove these members from the ZG_East_DM group?


  • As for ...

            I accidentally added over 400 users to the wrong Security Group.

    ... This is why -Whatif adn -Confirm exists. Rule number one, if you are just looking up stuff, go for it

    If you are doing anythign distructive (Create, Update/Modify, Delete aka CRUD code), never ever do this without validating that what you plan to do, is what will actually hapen.

    Yet, doing this...


    Remove-AdGroupMember -Identity "ZG_East_DM" -members $wrongmembers but it obviously didnt work

    ... and sayng this. ...

         Can anyone help me put together a ForEach-Object array

    ... which reall is a level 100 question, tranlsates into you are new to PowerShell and scripting in general. Looping (For/ForEach/ForEach-Object), these concepts exists in every progamming / scripting language ever created.

    There is no such thing as a ForEach-Object array. ForEach-Object is a method / action to act on objects, meaning, lists, arrays, etc.
            So, it's ...
                      Do this... (ForEach)
            ... to that
                      thing (Some file(s), List, Array, other collection object, like JSON, HTML, XML, etc.)
            ... for as many times as provided.

    It's OK to be new at X or Y, but one should not just guess at things and hope they will work. So, please spend sometime getting ramped up on PowerShell. You need to do this to limit / eliminate, misconceptions, poor code, bad habits, frustrating errors, destroying your host or you companies environment. Number one rule, never ever run anyone's code, no matter where you get it, unless you fully understand what it is doing. Especially, when you are doing destructive things like this.

    There are tons of free / no cost resources to leverage, all you need to do is invest time. See my Reddit posts here:


    Learning this stuff.

            https://www.reddit.com/r/PowerShell/comments/bserj9/learn_powershell/eooduq9/?context=3
            https://www.reddit.com/r/PowerShell/comments/bserj9/learn_powershell/eoodxzu/?context=3

    Best Practices
            https://www.reddit.com/user/get-postanote


    There are tons of similar post from many other fine folks as well. Yet, YouTube, MSDN Channel9 are the go to video locations for you to hit and loo up videos on PowerShell beginning/intermediate/advanced, form design/GUI, Arrays, Enumerations, etc...)

    See also:
            https://powershellexplained.com/2018-10-15-Powershell-arrays-Everything-you-wanted-to-know
            https://powershell.anovelidea.org/powershell/foreach-object-whatif

    Deciding on using For, ForEach or ForEach-Object is dependent on what you are trying to do. See the help files for their differences and sameness.


    As for what you are after you are trying to pass a collection, and the cmdlet is asking for a single identity as shown in the help examples for the cmdlet.
    So this...

                Import-CSv -Path 'C:\wrongmembers.txt' -Header SamAccountName |
                ForEach{
                    # Test removing misassigned group members. Remove the -WhatIf for the action to actually complete
                    Remove-AdGroupMember -Identity 'ZG_East_DM' -members $PSItem.SamAccountName -WhatIf
                }

    ... or this...

                Import-CSv -Path 'C:\wrongmembers.txt' |
                ForEach{
                    # Test removing mis-assigned group members. Remove the -WhatIf for the action to actually complete
                    Remove-AdGroupMember -Identity 'ZG_East_DM' -members $PSItem -WhatIf
                }

    • Always read the help info thoroughly.
    • Never, ever guess when dealing with destructive actions. You can / will destroy your host or your entire enterprise.
    • Always approach one step at a time to make sure you are getting / acting what / on you'd expect.
    • Always test before execute.