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.
Post Reply
User avatar
Aenergia
Posts: 2
Joined: Wed Feb 07, 2018 4:39 am

Copy folders with progress bar

Post by Aenergia » Thu Sep 12, 2019 8:39 am

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
Joined: Wed Feb 07, 2018 4:39 am

Re: Copy folders with progress bar

Post by Aenergia » Thu Sep 12, 2019 9:01 am

Nevermind...

Problem solved after starting PowerShell Studio with admin permissions.

User avatar
Alexander Riedel
Posts: 7017
Joined: Tue May 29, 2007 4:43 pm

Re: Copy folders with progress bar

Post by Alexander Riedel » Thu Sep 12, 2019 9:10 am

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.

Post Reply