Search code examples
powershellwinformslistbox

Create a Powershell ListBox Sticky Notes Backup Utility


I'm building a PowerShell utility that will backup and restore sticky notes using a powershell script to pull the Get-localuser command from a local device into the list box. From list box choose the user profile and it initiates a comand to fetch a file. What im researching is after you select a user in list box, it sends a command to copy a file Thank you.

Example: Admin is the user:

%SystemRoot%\explorer.exe c:\users\Admin\AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\pause copy plum.sqlite-wal cd C:\Users\Admin\Documents\SN Utility

working edited: code

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = 'StickyNotes Utility'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(10,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(185,120)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(200,20)
$label.Text = 'Select a user to backup:'
$form.Controls.Add($label)

$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(250,20)
$listBox.Height = 80

$listbox.Items.AddRange((get-localuser | select -expand name))

$form.Controls.Add($listBox) 

$form.Topmost = $True

do
{
    $result = $form.ShowDialog()

    if ($ListBox.SelectedIndices.Count -lt 1 -and $result -eq [System.Windows.Forms.DialogResult]::OK)
    {
        Write-Warning 'Nothing was selected, please select a user.'
    }
}
until (($result -eq [System.Windows.Forms.DialogResult]::OK -and $listBox.SelectedIndices.Count -ge 1) -or $result -ne [System.Windows.Forms.DialogResult]::OK)

{
    $x = $listBox.SelectedItem
    
    $x
}


directory list inside list box


$subfolders = (Get-ChildItem -Path $rootFolder -Recurse -Directory).FullName

Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form 
$form.Text = "SubFolders"
$form.Size = New-Object System.Drawing.Size(300,300) 
$form.StartPosition = "CenterScreen"

$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40) 
$listBox.Size = New-Object System.Drawing.Size(260,180)
$listBox.Anchor = 'Top,Right,Bottom,Left' 

$listbox.Items.AddRange((get-localuser | select -expand name))

$listBox.Add_Click({
    $selected = $listBox.GetItemText($listBox.SelectedItem)
        [System.Windows.Forms.MessageBox]::Show("You selected subfolder`r`n`r`n$selected", "Subfolder")
})

$form.Controls.Add($listBox) 

$form.ShowDialog()
$form.Dispose()


Solution

  • If I understand your question correctly, i believe you want to replace the line

    [void] $listBox.Items.AddRange(@("user1", "user2", "user3"))
    

    with

    Get-Localuser | select -expand name | foreach {[void] $listbox.Items.Add($_)}
    

    or

    $listbox.Items.AddRange((get-localuser | select -expand name))