question

ThomasGroh-5824 avatar image
0 Votes"
ThomasGroh-5824 asked LimitlessTechnology-2700 published

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

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered LimitlessTechnology-2700 published

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!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.