Home > Mobile >  How to display a PowerShell output in List view using Windows Forms
How to display a PowerShell output in List view using Windows Forms

Time:09-27

How do I output the Get-ADUser -Filter "GivenName -eq '$UserList' -or Surname -eq '$UserList'" | select Name, SamAccountName in to a list view.

List view Variable is = $lwOutput

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = New-Object System.Drawing.Point(547,386)
$Form.text                       = "Form"
$Form.TopMost                    = $false

$tbName                          = New-Object system.Windows.Forms.TextBox
$tbName.multiline                = $false
$tbName.width                    = 203
$tbName.height                   = 20
$tbName.location                 = New-Object System.Drawing.Point(218,37)
$tbName.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$lblName                         = New-Object system.Windows.Forms.Label
$lblName.text                    = "Enter First Name or Last Name"
$lblName.AutoSize                = $true
$lblName.width                   = 25
$lblName.height                  = 10
$lblName.location                = New-Object System.Drawing.Point(14,40)
$lblName.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$lwOutput                        = New-Object system.Windows.Forms.ListView
$lwOutput.text                   = "listView"
$lwOutput.width                  = 476
$lwOutput.height                 = 119
$lwOutput.location               = New-Object System.Drawing.Point(32,115)

$DGVOutput                       = New-Object system.Windows.Forms.DataGridView
$DGVOutput.width                 = 480
$DGVOutput.height                = 129
$DGVOutput.location              = New-Object System.Drawing.Point(31,248)

$btnSearch                       = New-Object system.Windows.Forms.Button
$btnSearch.text                  = "Search"
$btnSearch.width                 = 60
$btnSearch.height                = 30
$btnSearch.location              = New-Object System.Drawing.Point(361,71)
$btnSearch.Font                  = New-Object System.Drawing.Font('Microsoft Sans Serif',10,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))

$Form.controls.AddRange(@($tbName,$lblName,$lwOutput,$DGVOutput,$btnSearch))

$btnSearch.Add_Click({ btnGetUsers })

function btnGetUsers { 


$UserList = $tbName.Text
Get-ADUser -Filter "GivenName -eq '$UserList' -or Surname -eq '$UserList'" | select Name, SamAccountName 



}


#Write your logic code here

[void]$Form.ShowDialog()

Apologies for the basic question. Just trying out creating forms.

CodePudding user response:

Are you sure a plain grid view isn't sufficient?

Get-ADUser -Filter "GivenName -eq '$UserList' -or Surname -eq '$UserList'" | select Name, SamAccountName | Out-GridView

CodePudding user response:

Set the View properties of the listview to Details and add the two columns:

# add this to the ListView
$lwOutput.View                   = 'Details'
$null = $lwOutput.Columns.Add('Name',326)
$null = $lwOutput.Columns.Add('SamAccountName',150)

Then create your btnGetUsers function like this:

function btnGetUsers { 
    # remove whatever was in the ListView before
    $lwOutput.Items.Clear()
    # search for the user(s) and in a loop add the properties you need to the ListView
    Get-ADUser -Filter "GivenName -eq '$($tbName.Text)' -or Surname -eq '$($tbName.Text)'" | 
    ForEach-Object {
        $lvi = [System.Windows.Forms.ListViewItem]::new()
        $lvi.Text = $_.Name
        $lvi.SubItems.Add($_.SamAccountName)
        $lwOutput.Items.Add($lvi)
    }
}
  • Related