Home > Software engineering >  Powershell script is not displaying registry
Powershell script is not displaying registry

Time:02-04

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

$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(500,300)
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen

$label = New-Object System.Windows.Forms.Label
$label.Text = "Click the button to view the ProfileList registry in regedit"
$label.Size = New-Object System.Drawing.Size(300,30)
$label.Location = New-Object System.Drawing.Point(100,50)

$display_button = New-Object System.Windows.Forms.Button
$display_button.Text = "Display ProfileList"
$display_button.Size = New-Object System.Drawing.Size(150,30)
$display_button.Location = New-Object System.Drawing.Point(175,100)

$display_button.Add_Click({
regedit "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
})

$form.Controls.Add($label)
$form.Controls.Add($display_button)
$form.ShowDialog()

Upon running the script I receive the following error: "Error opening the file"

I am not sure if the path is correct, can someone point out what I'm doing wrong?

CodePudding user response:

Before opening regedit.exe, perform the following:

Set-ItemProperty 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Applets\Regedit' 'LastKey' 'Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList'

or

$splat = @{
    'Path'   = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Applets\Regedit'
    'Name'   = 'LastKey'
    'Value'  = 'Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList'
}
Set-ItemProperty @splat

and edit your button click actdion to simply:

$display_button.Add_Click({
    regedit
})
  • Related