Welcome › Forums › General PowerShell Q&A › Remove Groups partial group name
- This topic has 2 replies, 2 voices, and was last updated 1 month ago by
Participant.
-
AuthorPosts
-
-
December 13, 2020 at 6:43 pm #279282
Hey all
Anyway, I’m trying to get the below running. I guess I need to run this line against the foreach:
PowerShell1$Group = Get-ADGroup -filter {name -like "License_Assign*"} | Select nameThen once I have that value stored, I can run:
PowerShell1Remove-adgroupmember -Identity "$Group" -members $user.samaccountname -Confirm:$trueI just have no idea how to do that, any ideas?
Thanks all
PowerShell12345$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}-
This topic was modified 1 month, 1 week ago by
liminal162.
-
This topic was modified 1 month, 1 week ago by
liminal162.
-
This topic was modified 1 month, 1 week ago by
liminal162.
-
This topic was modified 1 month, 1 week ago by
-
December 14, 2020 at 9:15 am #279459
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.
PowerShell123$users = Import-Csv "C:\Scripts_CSV\Licences_to_Remove_Output.csv"Get-ADGroup -filter {name -like "License_Assign*"} |Remove-adgroupmember -members $users.samaccountname -Confirm -
December 15, 2020 at 5:31 pm #280014
Sorry mate, I missed this. Thanks for getting back to me 🙂
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
-
-
AuthorPosts
- You must be logged in to reply to this topic.