Rossi
Rossi

Reputation: 23

Display progress bar for 'reg copy' command

I wonder I anyone has an idea how can I add progress bar for multiple REG Copy items please?

Explorer UI works fine with Copy-Item but does not work with reg copy.

The reg file I copy is quite chunky and takes a while to copy so I thought some sort of progress bar would help. This is the example of my script for which I try to get a progress bar for:

$TestPath = "Registry::HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers"
    if ( Test-Path -path $TestPath  )  {          
    reg copy "\\$SourceHost\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers" "\\$DestHost\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers" /s /f | out-null
    }
    Else {
    reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers" /f | out-null
    reg copy "\\$SourceHost\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers" "\\$DestHost\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers" /s /f  | out-null
    }


    $TestPath = "Registry::HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\Print\Providers"
    if ( Test-Path -path $TestPath  )  {  
    reg copy "\\$SourceHost\HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\Print\Providers" "\\$DestHost\HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\Print\Providers" /s /f | out-null
    }
    Else {
    reg add "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\Print\Providers" /f | out-null
    reg copy "\\$SourceHost\HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\Print\Providers" "\\$DestHost\HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\Print\Providers" /s /f | out-null
    }
...

Upvotes: 1

Views: 258

Answers (5)

Rossi
Rossi

Reputation: 23

Got an idea just don't know if can be used i this situation.

In the initial post I have 2 reg copy jobs,

Can they be wrapped under 1 something like

$AJob = { Reg copy1 ... reg copy2 ... reg copy3 ... ... }

And then this $AJob is run thought GUI progress bar?

Upvotes: 0

Jonathan Perry
Jonathan Perry

Reputation: 141

If you have Powershell 5.0 on the computer where you are running the commands you could do something like this.

$SourceSession = New-PSSession -ComputerName $SourceHost
$DestSession = New-PSSession -ComputerName $DestHost

$RegPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers"
Copy-Item -FromSession $SourceSession -ToSession $DestSession -Path $RegPath -Destination $RegPath -Recurse -Force

Remove-PSSession $SourceSession
Remove-PSSession $DestSession

Assuming your source and destination computers stay the same you can reuse the same session variables until you're done.

Upvotes: 0

No Refunds No Returns
No Refunds No Returns

Reputation: 8356

If you are willing to parameterize your data a little bit you can use something similar to the following snippet. The CopyInfos contain all of your parameters. Presumably your inputs all follow a similar pattern name. The first line is the property name for each copy operation. The rest of the lines are pipe (|) delimited operations for each copy. Using the suggestions above you can compute the percent complete based on your parameters. From here you just need to plug in your correct parameters and build your correct strings for the copy and add functions.

$CopyInfos = ("Path1|Path2|Path3", `
    "Print\Providers|Source1|Destination", `
    "BowWowWow\Print\Providers|Source2|Destination2", `
    "Meow\Print\Providers|Source13|Destination3", `
    "Ow!\Print\Providers|Source14|Destination44", `
    "Wow6432Node\Print\Providers|Source15|Destination555" 
     ) | ConvertFrom-CSV -Delimiter "|"

$x = 0
foreach ($CopyInfo in $CopyInfos) {
  $percent = [int] (($x * 100) / $CopyInfos.Count)
  write-progress -Activity "$($CopyInfo.Path1)" -Status "Starting" -PercentComplete $Percent
  Start-Sleep -Seconds 2

  $x++
  $percent = [int] (($x * 100) / $CopyInfos.Count)
  write-progress -Activity "$($CopyInfo.Path1)" -Status "Finished" -PercentComplete $Percent
  Start-Sleep -Milliseconds 250
}

Upvotes: 0

postanote
postanote

Reputation: 16116

We know loops can be inefficient, but sometimes there a necessary thing. How about something like.

$TargetRegKeys = 'Registry::HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers',
'Registry::HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\Print\Providers'  

$TargetRegKeys | ForEach-Object -Begin {Clear-Host; $I = 0; $out = ""} -process { 
{
    # if ( Test-Path -path $_ ) { reg copy "\\$SourceHost\$_"  "\\$DestHost\$_" / s /f | out-null }
    # Else { reg copy "\\$SourceHost\$_"  "\\$DestHost\$_" / s /f | out-null }
}

$I = $I + 1
Start-Sleep -Seconds 3

Write-Progress -Activity "$($TargetRegKeys.Count) registry keys to process" -Status "Progress:" -PercentComplete ($I / $TargetRegKeys.Count * 100)
Start-Sleep -Seconds 3
} -end {$out}

Of course the Sleeps are only there for testing, so one can see the progress bar. The 'If' is commented to avoid any changes to my environment.

Upvotes: 1

vonPryz
vonPryz

Reputation: 24091

As like James C. already mentioned, reg doesn't support progress indicator. Powershell has, however, Write-Progress that can be used. Like so,

write-progress -activity "Updating registry" -status "Working: " -percentComplete 20
reg copy ...
write-progress -activity "Updating registry" -status "Working: " -percentComplete 40
reg copy ...
write-progress -activity "Updating registry" -status "Working: " -percentComplete 60
reg copy ...
write-progress -activity "Updating registry" -status "Working: " -percentComplete 80
reg copy ...

In this particular case, you have to decide the steps and increments in manual a way. The completion steps are up to you, so maybe something like:

$TestPath = "Registry::HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers"
write-progress -activity "Updating registry" -status "Working: " -percentComplete 20
if ( Test-Path -path $TestPath  )  {          
    reg copy ...
}
Else {
    reg add ...
    write-progress -activity "Updating registry" -status "Working: " -percentComplete 25
    reg copy ...
}

write-progress -activity "Updating registry" -status "Working: " -percentComplete 40
$TestPath = "Registry::HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\Print\Providers"
if ( Test-Path -path $TestPath  )  {  
    reg copy ...
}
Else {
    reg add ...
    write-progress -activity "Updating registry" -status "Working: " -percentComplete 50
    reg copy ...
}

Upvotes: 0

Related Questions