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
Get Installed Software to ListBox
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.
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.
-
- Posts: 14
- Last visit: Thu Feb 22, 2024 7:41 am
- Alexander Riedel
- Posts: 8488
- Last visit: Tue Apr 16, 2024 8:42 am
- Been upvoted: 37 times
Re: Get Installed Software to ListBox
[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
- Alexander Riedel
- Posts: 8488
- Last visit: Tue Apr 16, 2024 8:42 am
- Been upvoted: 37 times
Re: Get Installed Software to ListBox
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.
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.
SAPIEN Technologies, Inc.
Re: Get Installed Software to ListBox
i never use $global:..... it could lead to issues between scripts in the same powershell session
i always use $script:...
i always use $script:...
- Alexander Riedel
- Posts: 8488
- Last visit: Tue Apr 16, 2024 8:42 am
- Been upvoted: 37 times
Re: Get Installed Software to ListBox
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.
So using $global is a surefire way to determine if it is a scope issue.
Alexander Riedel
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
-
- Posts: 14
- Last visit: Thu Feb 22, 2024 7:41 am
Re: Get Installed Software to ListBox
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: 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
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: 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