How to choose user and navigate into user Documents folder powershell list box

Thomas Groh 1 Reputation point
2022-04-22T23:00:42.193+00:00

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 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 ends a command to copy a file from user profile folder. 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

code

$form = New-Object System.Windows.Forms.Form 
$form.Text = "Sticky Notes Backup"
$form.Size = New-Object System.Drawing.Size(300,250) 
$form.StartPosition = "CenterScreen"

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(10,180)
$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(85,180)
$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(280,20) 
$label.Text = "Please select user"
$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(150,20) 
$listBox.Height = 140

$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)
{
    $Prod = $listBox.SelectedItem
    $Prod
}
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,451 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 39,506 Reputation points
    2022-04-28T15:52:47.037+00:00

    Hi ThomasGroh-5824,

    Thanks for writing the scenario and attaching the script. We are happy to help you.

    Please find the working code snippet below. I have added code to copy the folders based on user selection under 'Process' method. If you have to further functionality, please add under 'end' method.

    ===============================Code starts here================
    [CmdletBinding(SupportsShouldProcess=$True)]

    Param ([Parameter(
    Mandatory = $False,
    ValueFromPipeline = $True,
    ValueFromPipelineByPropertyName = $True)]
    [Alias("A")]
    [Switch] $UserAccount,

    [Parameter(
    Mandatory = $False,
    ValueFromPipeline = $True,
    ValueFromPipelineByPropertyName = $True)]
    [Alias("CU")]
    [Switch] $SourcePath)

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

    $form = New-Object System.Windows.Forms.Form
    $form.Text = "Sticky Notes Backup"
    $form.Size = New-Object System.Drawing.Size(300,250)
    $form.StartPosition = "CenterScreen"
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(10,180)
    $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(85,180)
    $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(280,20)
    $label.Text = "Please select user"
    $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(150,20)
    $listBox.Height = 140
    $listbox.Items.AddRange((get-localuser | select -expand name))
    $form.Controls.Add($listBox)
    $form.Topmost = $True
    $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.'
    }
    ElseIf ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
    $UserAccount = $listBox.SelectedItem
    }
    }
    Process{
    If ($UserAccount -eq 'ravi2_000')
    {
    Write-Host = "Selected user account: " $UserAccount
    $str1="C:\Users\"
    $str2="\AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState"
    $SourcePath = $str1 + $UserAccount + $str2
    Write-Host = $SourcePath
    Copy-Item -Path $SourcePath -Destination "C:\Users\ravi2_000\OneDrive\Desktop\Fold" -recurse -Force
    }
    }
    End{
    If ($UserAccount -eq 'ravi2_000')
    {Write-Host = "Files copied from user account: " $UserAccount}
    }

    ==========Code ends here==============

    Please modify the 'If' condition based on your functionality (To validate whether correct user is selected)


    --If the reply is helpful, please Upvote and Accept as answer.--

    Thank you!

    0 comments No comments