Get Installed Software to ListBox

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
Scott Rosenbloom
Posts: 14
Last visit: Thu Feb 22, 2024 7:41 am

Get Installed Software to ListBox

Post by Scott Rosenbloom »

PowerShell Studio 2022:
Operating system: Windows 10
PowerShell version(s):

Hey All,

I have the following code to generate a list of installed software on a PC:

# Set the computer name equal to a variable.
$PCname = $env:ComputerName

# Set the current username equal to a variable
$PCuser = $env:UserName

# Set the Registry path "A" to a variable
$regPathA = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall"

# Set the children of Registry path "A" to a variable.
$InstAppsA = Get-ChildItem $regPathA

# For each child found within Registry path "A"...
foreach ($obj in $InstAppsA)
{
# Create array for Excel with column names and values
$process = @(
[pscustomobject]@{
"Run by" = $PCuser
"Registry Path" = $regPathA
"Software Name" = $obj.GetValue('DisplayName')
"Software Version" = $obj.GetValue('DisplayVersion')
}
)

$process
}

When I run this, it outputs the list to the PowerShell Console. I'm trying to get it so I click a button on a form and have it populate a list box. To do that, I have what I'd think would work as the following:

$form1_Load={
#TODO: Initialize Form Controls here

}

$btnGetSoftware_Click= {
#TODO: Place custom script here
# For each child found within Registry path "A"...
foreach ($obj in $InstAppsA)
{
# Create array for Excel with column names and values
$process = @(
[pscustomobject]@{
"Run by" = $PCuser
"Registry Path" = $regPathA
"Software Name" = $obj.GetValue('DisplayName')
"Software Version" = $obj.GetValue('DisplayVersion')
}
)


# Export array to "processes.xlsx" Excel file
$listbox1.Text = $process
}

}

I click the button, but nothing populates. What am I missing?

Thanks,
Scott
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Get Installed Software to ListBox

Post by Alexander Riedel »

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Get Installed Software to ListBox

Post by Alexander Riedel »

Most likely you have a scope problem or an initialization problem. You can easily find out by setting a breakpoint at foreach ($obj in $InstAppsA) and examine $InstAppsA in the debugger.
I would assume that it is empty, as you get no output. I cannot see from your code where you initialize that, but you either didn't or did that in a different scope.
If you are initializing it somewhere, change all instances of $InstAppsA to $global:InstAppsA and see if that helps.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: Get Installed Software to ListBox

Post by Domtar »

i never use $global:..... it could lead to issues between scripts in the same powershell session

i always use $script:...
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Get Installed Software to ListBox

Post by Alexander Riedel »

That's a good policy. In this particular case I do not know how his code is organized. It could be that the initialization is in a different script.
So using $global is a surefire way to determine if it is a scope issue.
Alexander Riedel
SAPIEN Technologies, Inc.
Scott Rosenbloom
Posts: 14
Last visit: Thu Feb 22, 2024 7:41 am

Re: Get Installed Software to ListBox

Post by Scott Rosenbloom »

Hey All,

I've gotten a little closer with the following code:

$regPathA = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall"

$InstAppsA = Get-ChildItem $regPathA

$foundApps = @()

foreach ($obj in $InstAppsA)
{
$process = $null
$process = [pscustomobject]@{
RegistryPath = $regPathA
Name = $obj.GetValue('DisplayName')
Version = $obj.GetValue('DisplayVersion')
}
$foundApps += $process
}

$form1_Load={
#TODO: Initialize Form Controls here
}

$btnGetSoftware_Click= {
$textbox1.Text = $foundApps | Out-String
}

When I click the button on the form, it outputs the following to a TEXTBOX:
2022-10-24_15-14-19.png
2022-10-24_15-14-19.png (48.53 KiB) Viewed 939 times
How would I format it to clean up how it's presented? Is a textbox the right element to display it within in and, if not, what would be better?

Thanks again,
Scott
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get Installed Software to ListBox

Post by jvierra »

I recommend outputting to a DataGridView by adding everything you want to display to a DataTable bound to a DGV.
This topic is 1 year and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked