Write a script to create a programme on Windows 7:
Create an 800x600 window.
Inside this window, a complete list of installed programmes should be displayed.
Assistant
Certainly! You can create a simple PowerShell script that utilizes Windows Forms to create a graphical window and display a list of installed programs on a Windows 7 system. Below is a step-by-step guide along with the complete script.
Step-by-Step Guide
Ensure PowerShell is Available:
Windows 7 comes with PowerShell pre-installed. However, for enhanced features, consider using PowerShell 5.1. You can check your PowerShell version by running $PSVersionTable.PSVersion in the PowerShell console.
Understanding the Script Components:
Add-Type: This cmdlet allows you to add .NET Framework types to your PowerShell session, which is essential for creating Windows Forms.
Form Creation: Define the properties of the main window, such as size, title, and layout.
List Retrieval: Fetch the list of installed programs from the Windows Registry.
Display Control: Use a ListBox to present the list of programs within the window.
Run the Script:
Save the script with a .ps1 extension, for example, ListInstalledPrograms.ps1.
Right-click the script file and select "Run with PowerShell," or execute it within a PowerShell window.
Execution Policy:
If you encounter execution policy restrictions, you might need to adjust them. You can set the policy to allow script execution by running PowerShell as an administrator and executing:
powershell
Set-ExecutionPolicy RemoteSigned
Note: Be cautious when changing execution policies. Ensure you understand the implications for system security.
Complete PowerShell Script
powershell
# Ensure the script runs with the required privileges
# You might need to run PowerShell as an administrator
# Add necessary assemblies for Windows Forms
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Create a new Windows Form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Installed Programs"
$form.Size = New-Object System.Drawing.Size(800, 600)
$form.StartPosition = "CenterScreen"
# Create a ListBox to display the programs
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Dock = "Fill"
$listBox.Font = New-Object System.Drawing.Font("Segoe UI", 10)
# Add the ListBox to the form
$form.Controls.Add($listBox)
# Function to retrieve installed programs from the registry
function Get-InstalledPrograms {
$uninstallPaths = @(
"HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*",
"HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*",
"HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
)
$programs = @()
foreach ($path in $uninstallPaths) {
if (Test-Path $path) {
Get-ItemProperty $path -ErrorAction SilentlyContinue | ForEach-Object {
if ($_.DisplayName) {
$programs += $_.DisplayName
}
}
}
}
return $programs | Sort-Object
}
# Retrieve the list of installed programs
$installedPrograms = Get-InstalledPrograms
# Add the programs to the ListBox
foreach ($program in $installedPrograms) {
$listBox.Items.Add($program)
}
# Optional: Add a search box to filter programs
# Create a TextBox for search
$searchBox = New-Object System.Windows.Forms.TextBox
$searchBox.Width = 200
$searchBox.Location = New-Object System.Drawing.Point(10, 10)
$searchBox.Anchor = "Top, Left"
# Add the Search Box to the form
$form.Controls.Add($searchBox)
# Create a Label for the search box
$searchLabel = New-Object System.Windows.Forms.Label
$searchLabel.Text = "Search:"
$searchLabel.AutoSize = $true
$searchLabel.Location = New-Object System.Drawing.Point(10, 35)
$searchLabel.Anchor = "Top, Left"
$form.Controls.Add($searchLabel)
# Adjust ListBox position to accommodate search box
$listBox.Location = New-Object System.Drawing.Point(10, 55)
$listBox.Size = New-Object System.Drawing.Size(760, 500)
# Implement the search functionality
$searchBox.Add_KeyUp({
$filter = $searchBox.Text.ToLower()
$listBox.Items.Clear()
foreach ($program in $installedPrograms) {
if ($program.ToLower().Contains($filter)) {
$listBox.Items.Add($program)
}
}
})
# Show the form
[void]$form.ShowDialog()
Script Explanation
Assemblies Import:
System.Windows.Forms and System.Drawing are necessary for creating the GUI elements.
Form Setup:
A form titled "Installed Programs" is created with a size of 800x600 pixels and centered on the screen.
ListBox Creation:
A ListBox is docked to fill the form, displaying the list of installed programs with a readable font.
Installed Programs Retrieval:
The Get-InstalledPrograms function queries the registry paths where installed programs are typically listed:
HKLM (HKEY_LOCAL_MACHINE) for all users.
Wow6432Node for 32-bit applications on a 64-bit system.
HKCU (HKEY_CURRENT_USER) for user-specific installations.
It gathers the DisplayName property of each installed program and sorts the list alphabetically.
Search Functionality (Optional):
A TextBox and Label are added to allow users to search/filter the list of installed programs in real-time.
As the user types in the search box, the ListBox updates to show matching entries.
Displaying the Form:
The form is rendered and displayed to the user with [void]$form.ShowDialog().
Running the Script
Save the Script:
Copy the above script into a text editor and save it as ListInstalledPrograms.ps1.
Execute the Script:
Right-click the saved .ps1 file and select "Run with PowerShell."
Alternatively, open PowerShell, navigate to the script's directory, and execute:
powershell
.\ListInstalledPrograms.ps1
Permissions:
If prompted about script execution policies, ensure you've set the appropriate policy as mentioned earlier.
Using the Program:
An 800x600 window titled "Installed Programs" will appear, listing all installed applications.
Use the search box at the top to filter programs by name.
Notes
Performance Considerations:
Retrieving installed programs from the registry is generally fast, but on systems with a large number of installed applications, there might be a slight delay.
Enhancements:
You can expand the script to show additional details like version numbers, installation dates, or even uninstall capabilities by extending the ListBox items or using a more advanced control like DataGridView.
Compatibility:
This script is tailored for Windows 7 but should work on other Windows versions with PowerShell installed.
Feel free to customize and enhance the script further to fit your specific needs!