Remove Groups partial group name

Hey all

Anyway, I’m trying to get the below running. I guess I need to run this line against the foreach:

$Group = Get-ADGroup -filter {name -like "License_Assign*"} | Select name

Then once I have that value stored, I can run:

Remove-adgroupmember -Identity "$Group" -members $user.samaccountname -Confirm:$true

I just have no idea how to do that, any ideas?

Thanks all

$users = Get-Content "C:\Scripts_CSV\Licences_to_Remove_Output.csv"
$Group = Get-ADGroup -filter {name -like "License_Assign*"} | Select name
$users | ForEach-Object {
Remove-adgroupmember -Identity "$Group" -members $user.samaccountname -Confirm:$true
}

 

 

 

 

What does C:\Scripts_CSV\Licences_to_Remove_Output.csv contain? Just need the format. Get-Content would only get raw strings for each line of text in that csv. You probably want Import-Csv so you can create objects for each line and then you can use the property dereference operator “.” to access the individual properties like $user.samaccountname. Making an assumption that samaccountname is the column heading in this example. Also the members property will take a collection (array) of objects so no need for the foreach loop. Also, the Identity parameter will take pipeline input so you should be able to pipe right into Remove-ADGroupMember. I can’t test in my environment however.

$users = Import-Csv "C:\Scripts_CSV\Licences_to_Remove_Output.csv"
Get-ADGroup -filter {name -like "License_Assign*"} |
    Remove-adgroupmember -members $users.samaccountname -Confirm

 

 

Sorry mate, I missed this. Thanks for getting back to me :slight_smile:

It is a column with samaccountname. I was just trying to get some general information yesterday about Get-Content vs Import-CSV, thats very interesting thanks!

Also not needing the foreach, it just seems to be the go to for anything involving a CSV now.

Anyway, ill test that and let you know how it goes.

Thanks again