Script powershell in two sections

Diego M 1 Reputation point
2021-02-18T14:29:36.457+00:00

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}

}


$result;

#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,574 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,306 Reputation points
    2021-02-18T15:31:33.83+00:00

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

    $selection = 0
    Do{
        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
    $result
    Do{
        $utente = Read-Host "please enter NAME to to remove user from group $group, or 'Q' to quit"
        if ($utente -eq 'Q'){exit}
    } while ($result.name -notcontains $utente)
    Remove-ADGroupMember -identity $group -members $utente -confirm
    
    0 comments No comments

  2. Ian Xue 38,301 Reputation points Microsoft Vendor
    2021-02-19T11:26:01.207+00:00

    Hi,

    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.