Copy folders with progress bar

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Aenergia
Posts: 2
Last visit: Mon Feb 03, 2020 4:32 am

Copy folders with progress bar

Post by Aenergia »

Hi,

I made a simple dialog box to copy profile folders from one local user to another, using robocopy:

Image

Code: Select all

$formProfileMigration_Load= {
	
	# Get Local Profile List and Enumerate Source Profile
	$ProfileList = Get-WmiObject -ComputerName $env:COMPUTERNAME Win32_UserProfile -filter "LocalPath Like 'C:\\Users\\%' AND special=False AND loaded=False" -ea stop | Select LocalPath
	foreach ($User in $ProfileList)
	{
		Update-ComboBox $SourceBox ($User).LocalPath -Append
		Update-ComboBox $DestinationBox ($User).LocalPath -Append
	}
	
}
	
$DestinationBox_SelectedIndexChanged = {
		
	if ($SourceBox.SelectedItem -eq $DestinationBox.SelectedItem)
	{
		Add-Type -AssemblyName PresentationCore, PresentationFramework
		$ButtonType = [System.Windows.MessageBoxButton]::OK
		$MessageboxTitle = "Warning"
		$Messageboxbody = "Cannot copy into the same profile"
		$MessageIcon = [System.Windows.MessageBoxImage]::Warning
		[System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $messageicon)
	}
}

$buttonStart.add_Click({
		
		# Error message in case there's no selected profile
		if ($SourceBox.SelectedItem -eq $null -or $DestinationBox.SelectedItem -eq $null)
		{
			Add-Type -AssemblyName PresentationCore, PresentationFramework
			$ButtonType = [System.Windows.MessageBoxButton]::OK
			$MessageboxTitle = "Warning"
			$MessageboxBody = "Please select a Profile"
			$MessageIcon = [System.Windows.MessageBoxImage]::Warning
			[System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $messageicon)
		}
		
		# Set profile folders to copy
		$FoldersToCopy = @(
			"AppData\Local\Google"
			"Desktop"
			"Documents"
			"Downloads"
			"Favorites"
			"Pictures"
			"Videos"
		)
		
		# Setup progress bar
		$progressbaroverlay1.Maximum = $FoldersToCopy.Count
		
		# Set source and destination folders
		$SourceRoot = "$($SourceBox.SelectedItem)"
		$DestinationRoot = "$($DestinationBox.SelectedItem)"
		
		# Copy folders and update the progress bar
		foreach ($Folder in $FoldersToCopy)
		{
			
			$Source = Join-Path -Path $SourceRoot -ChildPath $Folder
			$Destination = Join-Path -Path $DestinationRoot -ChildPath $Folder
			
			robocopy.exe $Source $Destination /E /IS /NP /NFL
			$progressbaroverlay1.PerformStep()
		}
		
		
	})
After I click the Start button, the form freezes:

Image

And I only have 3 folders copied:

Image

I have to terminate the process manually in Task Manager.
When I press terminate in Task Manager, the progress bar gets filled and the window closes.
I have no errors/messages in the Output window.

How can I find out what's causing the freeze?

Thank you
User avatar
Aenergia
Posts: 2
Last visit: Mon Feb 03, 2020 4:32 am

Re: Copy folders with progress bar

Post by Aenergia »

Nevermind...

Problem solved after starting PowerShell Studio with admin permissions.
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: Copy folders with progress bar

Post by Alexander Riedel »

You can launch any script elevated by using the elevation button on the ribbon. There is no need to run PowerShell Studio itself elevated at all times.
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 4 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked