If user is in Group A then remove them and add to Group B

Hi I wondered if anybody could help me? I need to add a section to a script where it checks if a single user which will be picked up earlier in the script as $user1 for example and then checks if they are a certain group and if they are then remove them from the group and add them to another. Not got my head around IF arguments yet.

I would suggest reading the “about if” documentation as your starting point. Practice with some very simple uses of what if. If you run into an issue, post your code and someone will give you a pointer to get past the issue.

If you have some code for your project around your posted question and are stuck, share along with as much detail for your goal as possible. Someone is likely to give you hand moving forward.

All links are for PS 5.1 which is built into windows. You can switch the documentation to the version of PS that best matches the version you are using. Though the core functionality is pretty stable and 5.1 will cover most of your learning needs.

about If - PowerShell | Microsoft Learn

Some other PS documentation that might be useful to this project.

cmdlets that give some information about a group a user is a member of. Which to use really depends on what data you are wanting and goals of the script.

Get-ADPrincipalGroupMembership (ActiveDirectory) | Microsoft Learn

Get-ADGroupMember (ActiveDirectory) | Microsoft Learn

Get-ADUser (ActiveDirectory) | Microsoft Learn

If statements are basically a test if something is true or not. Is user01 a member of a Group01? True? False? Comparison is one test that can be done with an IF statement.

about Comparison Operators - PowerShell | Microsoft Learn

Regular Expressions (regex) can be handy in IF statements too, though for this project you probably won’t need it. But good skill to build just the same.

about Regular Expressions - PowerShell | Microsoft Learn

The Where-Object cmdlet is something you will likely need for this project.

Where-Object (Microsoft.PowerShell.Core) - PowerShell | Microsoft Learn

If you have a list of users to check, then a foreach loops will be useful

about Foreach - PowerShell | Microsoft Learn