How to run code in the powershell script for selected checkboxes

I have a ps script that extracts applied GPO’s from a specific OU into a CSV file and loads the name of the GPO value into a form with check list next to each GPO name and then i can select any GPO available and either select to enable or disable this GPO for a user by entering the username in the next form.

My problem is when i select multiple GPO’s to apply for a user the code runs against only one GPO(Last selected) and not for all selected, how is it possible to invoke the command on all selected GPO from the form, and is there a way after i finish entering the username the first form would re-open to go through the process again to select other GPO’s for other users?

EDIT: Solved thank you .

Just like you looped to get values from a CSV, you need to loop to get items from the listbox. Not tested, but try something like:

    $us = [Microsoft.VisualBasic.Interaction]::InputBox("Select Username", "User Name")
    
    $results = foreach ($si in $listBox.SelectedItem) {
        try {
            
            $params = @{
                Name            = $si.Value
                PermissionLevel = 'GpoApply'
                TargetName      = $us
                TargetType      = 'User'
                Domain          = $Domain.Name
                ErrorAction     = 'Stop'
            }

            Set-GPPermissions @params

        }
        catch {
            'Ut oh. Issue setting GP Permission for {0}. {1}' -f $si, $_
        }
    }

Hi Rob,

I have tested your method and unfortunately, it returns no errors but does nothing in return.