Script powershell in two sections

Diego M 1 Reputation point

I have this script to show members of AD groups and later i would to select user to delete from a group.
This script interrupts after switch statement. Without the second part (after comment) script go on and display results of switch query.
After $group ... the execution break after $result without display get-aduser.
Where is the problem? thanks everyone

write-Host "Select group"
write-Host "1 = group1"
write-Host "2 = group2"
write-Host "3 = group3"

$selection = Read-Host "please select"
$result = switch ($selection) 

1 {Get-ADGroupMember -Identity group1 | Get-ADUser | Select-Object givenname, surname, name}
2 {Get-ADGroupMember -Identity group2| Get-ADUser | Select-Object givenname, surname, name}
3 {Get-ADGroupMember -Identity group3| Get-ADUser | Select-Object givenname, surname, name}



#here there is a problem for the script to continue

$group = read-host "please select group"
$utente = Read-Host "please select user to delete"
Remove-ADGroupMember -identity $grou -members $user -confirm
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,184 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 43,246 Reputation points

    I haven't tested this, but see if it fixes your problem:

    $selection = 0
        Write-Host "Select group"
        Write-Host "1 = group1"
        Write-Host "2 = group2"
        Write-Host "3 = group3"
        Write-Host "Q = Quit"
        $selection = Read-Host "please select"
    } While (1,2,3,"Q" -notcontains $selection) 
    switch ($selection) {
        1 { $group = 'group1' }
        2 { $group = 'group2' }
        3 { $group = 'group3' }
        'Q' { exit }
    $result = Get-ADGroupMember -Identity $group | 
                Get-ADUser | 
                    Select-Object givenname, surname, name
        $utente = Read-Host "please enter NAME to to remove user from group $group, or 'Q' to quit"
        if ($utente -eq 'Q'){exit}
    } while ($ -notcontains $utente)
    Remove-ADGroupMember -identity $group -members $utente -confirm
    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 24,461 Reputation points Microsoft Vendor


    Try adding Out-Host to $result

    $result | Out-Host  

    Best Regards,
    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments