Handling registry values in winform-treeview using powershell

Hi all,

I was fetching certain registry keys using power-shell in which certain keys  has to be  deleted for handling "printer bi-directional printing option being grayed out" issue. I was using treeview winform in powershell to display the keys.

$form = New-Object System.Windows.Forms.Form
$Form.ClientSize = '342, 502'
$Form.FormBorderStyle = 'FixedDialog'

$treeView = New-Object System.Windows.Forms.TreeView
$treeView.Dock = 'Fill'
$treeView.CheckBoxes = $true

$N1 = $treeView.Nodes.Add('Version-3') 
$N2 = $treeView.Nodes.Add('Monitors')
$N3 = $treeView.Nodes.Add('Printers')




$Directory = "HKLM:\system\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3\"
$datas = Get-ChildItem -path $Directory | Select -ExpandProperty Name 

foreach($data in $datas)

{
$s = $data.Split("\")

$node = $N1.Nodes.Add($s[9]) 
}

$Directory1 = "HKLM:\system\CurrentControlSet\Control\Print\Monitors\"
$datas1 = Get-ChildItem -path $Directory1 | Select -ExpandProperty Name 

foreach($data1 in $datas1)

{
$s = $data1.Split("\")

$node = $N2.Nodes.Add($s[6]) 
}



$Directory2 = "HKLM:\system\CurrentControlSet\Control\Print\Printers\"
$datas2 = Get-ChildItem -path $Directory2 | Select -ExpandProperty Name 

foreach($data2 in $datas2)

{
$s = $data2.Split("\")

$node = $N3.Nodes.Add($s[6]) 
}
#>


$TV_AfterCheck = 
{
 if($_.Node.Checked)
 {
  [System.Windows.Forms.MessageBox]::Show(
   'Node ' + $_.Node.Text + ' checked')
 }
 else
 {
  [System.Windows.Forms.MessageBox]::Show(
   'Node ' + $_.Node.Text + ' unchecked')
 }
}

$treeView.Add_AfterCheck($TV_AfterCheck)


$form.Controls.Add($treeView)
$form.ShowDialog()

Now I want to delete the keys which are checked using remove-itemproperty and remove accordingly . 

That means if a option is selected below version-3, it has to be deleted from path 

HKLM:\system\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3\

if a option is selected below Monitors it has to be deleted from path 

HKLM:\system\CurrentControlSet\Control\Print\Monitors\

Could you guys help me out. 

August 27th, 2015 4:00am

This is more direct:

$key='HKLM:\system\CurrentControlSet\Control\Print\Monitors'
Get-ChildItem $key | ForEach-Object{
    $n=$N2.Nodes.Add($_.PsChildName)
    $n.Tag=$_.Name
}

Now just retrieve the tag and use it in Remove-Item.

Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 4:57am

I think this is closer to what you are trying to do:

function Add-ChildNodes{
	Param($key)
	
	$name=Split-Path $key -Leaf
	$node=$treeview.Nodes.Add($name)
	$node.Tag=$key
	Get-ChildItem $key | 
    	ForEach-Object{
        	$n=$node.Nodes.Add($_.PsChildName)
        	$n.Tag=$_.Name
    	}
}


Add-Type -assembly System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$Form.ClientSize = '342, 502'
$Form.FormBorderStyle = 'FixedDialog'
$form.StartPosition='CenterScreen'

$treeView = New-Object System.Windows.Forms.TreeView
$form.Controls.Add($treeView)
$treeView.Dock = 'Fill'
$treeView.CheckBoxes = $true

Add-ChildNodes 'HKLM:\system\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3'
Add-ChildNodes 'HKLM:\system\CurrentControlSet\Control\Print\Monitors'
Add-ChildNodes 'HKLM:\system\CurrentControlSet\Control\Print\Printers'

$treeView.Add_AfterCheck({[System.Windows.Forms.MessageBox]::Show($_.Node.Tag,$(if($_.Node.Checked){'CHECKED'}else{'UNCHECKED'}))})
$treeView.ExpandAll()
$form.ShowDialog()

August 27th, 2015 5:50am

Thanks jrv,

I will check it out and revert to u about the results. Scripting guys forums always rockz....................

Free Windows Admin Tool Kit Click here and download it now
August 28th, 2015 12:07am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics