0

Hi I'm new to Powershell. I'm looking Get-WmiObject -Class Win32_Product output on a GUI Form. Thanks in Advance.

Below is the Code, I need to add label to the buttons and assign a function to each button.

Label: Uninstall Function: Uninstall Start-Process Powershell -verb runas # Load Windows Forms assembly [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void][System.Windows.Forms.Application]::EnableVisualStyles() # Create a GUI $form = New-Object System.Windows.Forms.Form $form.Size = New-Object System.Drawing.Size(920,500) $form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen $dataGridView = New-Object System.Windows.Forms.DataGridView $dataGridView.Size = New-Object System.Drawing.Size(900,400) $button = New-Object System.Windows.Forms.Button $button.Location = New-Object System.Drawing.Size(400,420) $button.Size = New-Object System.Drawing.Size(75,25) $button.text = "Uninstall" $form.Controls.Add($button) $form.Controls.Add($dataGridView) # Select appropriate columns $dataGridView.Columns.Insert(0, (New-Object System.Windows.Forms.DataGridViewButtonCell)) $dataGridView.ColumnCount = 8 $dataGridView.ColumnHeadersVisible = $true $dataGridView.Columns[0].Name = "Uninstall" $dataGridView.Columns[1].Name = "Description" $dataGridView.Columns[2].Name = "IdentifyingNumber" $dataGridView.Columns[3].Name = "Name" $dataGridView.Columns[4].Name = "Vendor" $dataGridView.Columns[5].Name = "Version" $dataGridView.Columns[6].Name = "Caption" $dataGridView.Columns[7].Name = "InstallLocation" $dataGridView.Columns[0].width = 40 $dataGridView.Columns[1].width = 200 # Get a list of items <#Get-WmiObject -Class Win32_Product | foreach { $dataGridView.Rows.Add($.Check,$.Description,$.IdentifyingNumber,$.Name,$.Vendor,$.Version,$.Caption,$.InstallLocation) | out-null }#> # Refresh function gridClick(){ $rowIndex = $dataGridView.CurrentRow.Index $columnIndex0 = $dataGridView.ColumnIndex+1 $columnIndex1 = $dataGridView.ColumnIndex+2 $columnIndex2 = $dataGridView.ColumnIndex+3 $columnIndex3 = $dataGridView.ColumnIndex+4 $columnIndex5 = $dataGridView.ColumnIndex+5 #Write-Host $rowIndex #Write-Host $columnIndex0 #Write-Host $dataGridView.Rows[$rowIndex].Cells[0].value Write-Host $dataGridView.Rows[$rowIndex].Cells[$columnIndex0].value Write-Host $dataGridView.Rows[$rowIndex].Cells[$columnIndex1].value Write-Host $dataGridView.Rows[$rowIndex].Cells[$columnIndex5].value #$IdentifyNumber = $dataGridView.Rows[$rowIndex].Cells[$ClassKey].value #$Name = $dataGridView.Rows[$rowIndex].Cells[$columnIndex0].value #$classKey = 'IdentifyingNumber=$IdentifyingNumber.value,Name=$Name.value,Version=$Version.value' #Write-Host $classKey #([wmi]”\$server\root\cimv2:Win32_Product.$classKey”).uninstall() } $Uninstall = $dataGridView.Add_CellMouseClick({gridClick}) # Show the form [void]$form.ShowDialog()

4
  • 1
    Which Powershell version? Tagging specific version means that solution that worksi only on that version is desired. As PS2/3/4 are quite different, tagging all those might not be relevant. Commented Jan 19, 2017 at 6:28
  • What kind of GUI form are you looking for, anyway? Would Out-GridView be good enough? Commented Jan 19, 2017 at 6:29
  • Thanks for the reply.. Out-Gridview is fair for me but Matt Szadziul ans is what i'm looking for. Commented Jan 20, 2017 at 9:07
  • Never use Win32_Product. You should instead query the registry directly for inventorying software on Windows. Win32_Product will result in integrity checks and repair installations when listing software, which can cause unintended outages or resource contention on systems with other critical applications. Not to mention unscheduled/unplanned changes. Commented Mar 24, 2022 at 14:25

2 Answers 2

3

You can use this method to see a GUI grid :

gwmi -Class win32_product | Out-GridView

and also you can get custom output like XML and CSV and json and other form and use special software for that .

Sign up to request clarification or add additional context in comments.

Comments

1

This might be a bit of an overkill but you can always create a customized GUI form and paint the output in a gridview control like this:

# Load Windows Forms assembly

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
[void][System.Windows.Forms.Application]::EnableVisualStyles()

# Create a GUI

$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(920,500)
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Size = New-Object System.Drawing.Size(900,400)
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Size(400,420)
$button.Size = New-Object System.Drawing.Size(75,25)
$button.Text = "Refresh"
$form.Controls.Add($button)
$form.Controls.Add($dataGridView)

# Select appropriate columns

$dataGridView.ColumnCount = 7
$dataGridView.ColumnHeadersVisible = $true
$dataGridView.Columns[0].Name = "Description"
$dataGridView.Columns[1].Name = "IdentifyingNumber"
$dataGridView.Columns[2].Name = "Name"
$dataGridView.Columns[3].Name = "Vendor"
$dataGridView.Columns[4].Name = "Version"
$dataGridView.Columns[5].Name = "Caption"
$dataGridView.Columns[6].Name = "InstallLocation"

$dataGridView.Columns[0].width = 240

# Get a list of items

Get-WmiObject -Class Win32_Product | foreach {
    $dataGridView.Rows.Add($_.Description,$_.IdentifyingNumber,$_.Name,$_.Vendor,$_.Version,$_.Caption,$_.InstallLocation) | out-null
}

# Refresh

$button.Add_Click({

    $dataGridView.Rows.Clear()

    start-sleep -s 1

Get-WmiObject -Class Win32_Product | foreach {
    $dataGridView.Rows.Add($_.Description,$_.IdentifyingNumber,$_.Name,$_.Vendor,$_.Version,$_.Caption,$_.InstallLocation) | out-null
}

})

# Add a cell click function

function cellClick(){
$rowIndex = $dataGridView.CurrentRow.Index
$columnIndex = $dataGridView.CurrentCell.ColumnIndex
$value = $dataGridView.Rows[$rowIndex].Cells[$columnIndex].value
write-host $value
}

$dataGridView.Add_CellMouseClick({cellClick})

# Show the form

[void]$form.ShowDialog()

3 Comments

Glad I could help :)
Hey Matt can you help me to read the individual data from the Grid for the Selected Cells. I've modified the script where I need to select the Item and Uninstall the App. I'm stuck with reading the selected item.
Hi Deep :) I added a function that retrieves the cell value of the current column and the current row. If you launch the script via ISE, the selected value will be written to the console (variable value). Hope that helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.