Hi,
I’ve written a script with switch statement to view users of AD groups.
Script works well but when I add some line to define Other variables for remove some user from group, script don’t execute statement section correctly. It doesn’t show users with statement get-adgroupmember.
Please can you tell me why script play in different way?

5 Spice ups

Nope we can not read minds

Please post the section of the script that causes you trouble and the error or issue you are having with it

If you post code, please use the ‘Insert Code’ button. Please and thank you!

codebutton_small.png

4 Spice ups
Write-Host "selection of groups"
Write-Host "1 group 1" 
Write-Host "2 group 2"
$selection = Read-Host "please select "
switch ($selection) {
1 {Get-ADGroupMember -identity "GROUP1" -Recursive | Get-ADUser -Property DisplayName | Select Name,ObjectClass,DisplayName} 
2 {Get-ADGroupMember -identity "GROUP2" -Recursive | Get-ADUser -Property DisplayName | Select ,ObjectClass,DisplayName} 
    
} 
$selection

$group = Read-Host "please select group"
$user = Read-Host "please select user"
Remove-ADGroupMember -Identity $group -Members $user
Confirm

Does running the commands by themselves work?

Yes

When adding this section, after switch statement script stops and prompts “please select group” without execute get-adgroupmember

$group = Read-Host "please select group"
$user = Read-Host "please select user"
Remove-ADGroupMember -Identity $group -Members $user
Confirm

So you’re trying to make a selection out of the members it returns from the the Get-ADUser cmd? What does this line return?

Get-ADGroupMember -identity "GROUP1" -Recursive | Get-ADUser -Property DisplayName | Select Name,ObjectClass,DisplayName

Is that actually working for you? Also, why are you prompting to select the group again when you’ve already selected it with the first prompt?

The selection is made to check members.

This code works well and show group1’s members.

Get-ADGroupMember -identity "GROUP1" -Recursive | Get-ADUser -Property DisplayName | Select Name,ObjectClass,DisplayName


```
The second section ($user $group) requests group and member for deleting operation.

```

I apologize, im just having trouble understanding what you’re trying to do with the script itself. So, first, you’re prompting the user for a selection of either group 1 or 2, and passing the selection to the switch statement, where it switches to the corresponding cmd. Okay, I get that part. So after it does the switch it just display a bunch of user name and properties from that selected group? Then, you just type in the name and the group again so it can be passed to the remove cmdlet?

After the switch script requests name and group to delete form.

So, summarizing, script show the group members (group are 12 not 2 as the example show) and after request which user have to be deleted from which group.

Hope it’s more clear now.

Yes, that’s a little more clear. So it doesn’t show the members when you run the command? Is that the issue when performing the switch?

When I run the script with this section, it doesn’t show list of users in the group performed by the switch statement

It seems to be interrupted by this section…

$group = Read-Host "please select group" $user = Read-Host "please select user" Remove-ADGroupMember -Identity $group -Members $user Confirm

To avoid making this more difficult, want to give this a try?

Write-Host "selection of groups"
Write-Host "1 group 1" 
Write-Host "2 group 2"
$selection = Read-Host "please select "

$user = switch ($selection) {
        1 {Get-ADGroupMember -identity "Group1" -Recursive | Get-ADUser -Property DisplayName | Select Name,ObjectClass,DisplayName,SamAccountName} 
        2 {Get-ADGroupMember -identity "Group2" -Recursive | Get-ADUser -Property DisplayName | Select Name,ObjectClass,DisplayName,SamAccountName}    
    } 
""
for($i=0; $i -lt $sw.count; $i++){
    Write-Host "$($i): $($sw.DisplayName[$i])"}

$num = Read-Host -Prompt "Select User"
$user = $user[$num].SamAccountName
""
$group = Read-Host "please select group"

Remove-ADGroupMember -Identity $group -Members $user -Confirm

We need to pass the samaccount name to Remove-ADGroupMember. This could be done much more fluently and easier to read but, I dont change too much of the code on purpose so you can still be able to read what you’ve come up with so far. Id recommend also passing the group name to the remove cmdlet

Please can you explain what is the value of the variable $sw ?

Thanks

@diegom3 ​, I apologize! i tried making it more readable and didn’t swap every variable. See below:

Write-Host "selection of groups"
Write-Host "1 group 1" 
Write-Host "2 group 2"
$selection = Read-Host "please select "

$user = switch ($selection) {
        1 {Get-ADGroupMember -identity "Group1" -Recursive | Get-ADUser -Property DisplayName | Select Name,ObjectClass,DisplayName,SamAccountName} 
        2 {Get-ADGroupMember -identity "Group2" -Recursive | Get-ADUser -Property DisplayName | Select Name,ObjectClass,DisplayName,SamAccountName}    
    } 
""
for($i=0; $i -lt $user.count; $i++){
    Write-Host "$($i): $($user.DisplayName[$i])"}

$num = Read-Host -Prompt "Select User"
$resu = $user[$num].SamAccountName
""
$group = Read-Host "please select group"

Remove-ADGroupMember -Identity $group -Members $resu -Confirm
1 Spice up

Any luck?

I have to try yet on the work place.
Thanks.

1 Spice up