Author Posts

September 18, 2016 at 12:07 am

I apologize. I don't remember the original authors name but I'm trying to understand how to enable the selection on multiple items within a listview to enable removing the selected AD Group or AD Groups from a Computer. I understand first you would remove the VB msg lines to allow for more than one item to be selected but from there I get lost. Thanks for any help in advance.

 $buttonRemoveADGROUP_Click={
       $computername = $textboxCN.text
       $vbmsg = new-object -comobject wscript.shell
       if ($list1.selecteditems.count -gt 1) { $vbmsg1 = $vbmsg.popup("You may only select one AD Group to remove at a time.", 0, "Error", 0) }
       elseif ($list1.selecteditems.count -lt 1) { $vbmsg1 = $vbmsg.popup("Please select an AD Group to remove.", 0, "Error", 0) }
       else
       {
             
             $exprString = '$list1.SelectedItems | foreach-object {$_.tag} | foreach-object {$_.name}'
             $endapp = invoke-expression $exprString
             $statusbar1.text = "AD GROUPS ON " + $computername.ToUpper() + " (REMOVING $($Endapp))"
             import-module activedirectory
             $uninapp = Remove-ADGroup1
                           
             $List1.items.Clear()
             $statusbar1.text = "AD GROUPS ON " + $computername.ToUpper() + " (REFRESHING...)"
             
             $software = Get-ADPrincipalGroupMembership (Get-ADComputer $computername) | select-object name
             $columnproperties = "Name", "Result"
             foreach ($app in $software)
             {
                    $item = new-object System.Windows.Forms.ListViewItem($app.name)
                    if ($app.InstallDate -ne $null)
                    {
                           $item.SubItems.Add($app.Result)
                    }
                    $item.Tag = $app
                    $list1.Items.Add($item) > $null
             }
             $statusbar1.text = "AD GROUPS ON " + $computername.ToUpper() + " (" + $software.count + ")"
       }
}
Function Remove-ADGroup1
{
       $computername = $textboxCN.text
       $dns = get-adcomputer $computername
       $b = $dns.distinguishedname
       Remove-ADPrincipalGroupMembership $b (Get-ADGroup -Filter * | where { $_.name -eq $Endapp }) -Confirm:$False
}

September 18, 2016 at 12:05 pm

Multiselect is the property and it should be true by default.


$form = New-Object System.Windows.Forms.Form
$form.Width = 400
$form.Height = 800

$listView = New-Object System.Windows.Forms.ListView
$listView.View = 'Details'
$listView.Width = 400
$listView.Height = 800
$listView.Columns.Add('Listview properties', -2)
$listView.Items.AddRange(($listview |gm).name)

$Form.Controls.Add($listView)
[void] $Form.ShowDialog()

September 18, 2016 at 4:19 pm

Thanks for the reply. How would I be able to run the function Remove-ADGroup1 for each item selected in the list view?

September 19, 2016 at 1:59 pm

$list1.selecteditems | foreach {#dosomething;}