none
Migration Script Assistance RRS feed

  • Question

  • Trying to add the write-progress to :

    Get-ChildItem -Path $MyDocuments | % { Copy-Item $_.fullname $target -Recurse -container -Force -Exclude $ExclusionList }

    Want to see the progress of the copy


    Jason Ostrowski



    Tuesday, July 31, 2018 5:55 PM

Answers

  • So your question is about how to use Write-Progress, then. Here's a short example.


    $activity = "Write-Progress Example"
    $total = 100
    $counter = 1
    Write-Progress $activity "Starting loop..."
    Start-Sleep 3
    1..$total | ForEach-Object {
      Start-Sleep -Milliseconds 100
      $percent = ($counter / $total) * 100 -as [Int]
      $params = @{
        "Activity"         = $activity
        "Status"           = $counter
        "CurrentOperation" = "Getting item {0} of {1}" -f $counter,$total
        "PercentComplete"  = $percent
        "Completed"        = $percent -eq 100
      }
      Write-Progress @params
      $counter++
    }


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Tuesday, July 31, 2018 7:15 PM
    • Unproposed as answer by jrv Tuesday, July 31, 2018 7:15 PM
    • Proposed as answer by jrv Tuesday, July 31, 2018 7:15 PM
    • Unproposed as answer by jrv Tuesday, July 31, 2018 7:15 PM
    • Proposed as answer by jrv Tuesday, July 31, 2018 7:15 PM
    • Marked as answer by Richard MuellerMVP, Moderator Tuesday, August 7, 2018 1:00 PM
    Tuesday, July 31, 2018 7:02 PM
    Moderator

All replies

  • Sorry, but we don't have the resources to reproduce your environment and rewrite/debug your script for you.

    For best assistance, pare down your script such that it contains only the minimum amount of code needed to reproduce the problem. Then describe the actual vs. desired behavior.

    Read the following post for some good links on how to ask a good question:

    This forum is for scripting questions rather than script requests


    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 31, 2018 6:03 PM
    Moderator
  • Understood... Here's what I really need assistance with:

    $MyDocuments = ($Home + '\' + "Documents" + '\')

    $USBdrive = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} |  %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} | %{$_.deviceid}

    $ExclusionList = @(
    "*.AVI",
    "*.AVS",
    "*.MOV",
    "*.MOVIE",
    "*.MOOV",
    "*.MPEG",
    "*.MPG",
    "*.QT",
    "*.QTM",
    "*.SCM",
    "*.M4V",
    "*.MP4",
    "*.WMV",
    "*.AA",
    "*.AIF",
    "*.AIFF",
    "*.IFO",
    "*.CDA",
    "*.MID",
    "*.MPE",
    "*.MUS",
    "*.MP1",
    "*.MP2",
    "*.MP3",
    "*.M4A",
    "*.M4P",
    "*.RA",
    "*.RAM",
    "*.WAV",
    "*.WMA",
    "*.TMP",
    "*.TEMP",
    "*.CHK",
    "*.MOZ",
    "*.SWP",
    "*.HFV",
    "*.HIB",
    '*.$$$',
    "*.ISO",
    "*.NRG",
    "*.NRI",
    "*.CIF",
    "*.GI",
    "*.lnk",
    "DataCopyLog.txt",
    "*.crdownload"
    )

    $fileMD = Get-ChildItem -Path $MyDocuments -Recurse
    $i=1
    $MyDocuments | %{
    [int]$percent = $i / $MyDocuments.count * 100
    Write-Progress -activity "Copying MyDocuments Folder  ... ($percent %)" -PercentComplete $percent -verbose
    Copy-Item $fileMD.fullname -Destination ($USBdrive + $user + "\" + "MyDocuments") -Recurse -container -Force -Exclude $ExclusionList
    $i++
    }

    Can't get the progress bar to correctly reflect the progress and sometime I get an error that I exceeded the 100 limit


    Jason Ostrowski

    Tuesday, July 31, 2018 6:33 PM
  • So your question is about how to use Write-Progress, then. Here's a short example.


    $activity = "Write-Progress Example"
    $total = 100
    $counter = 1
    Write-Progress $activity "Starting loop..."
    Start-Sleep 3
    1..$total | ForEach-Object {
      Start-Sleep -Milliseconds 100
      $percent = ($counter / $total) * 100 -as [Int]
      $params = @{
        "Activity"         = $activity
        "Status"           = $counter
        "CurrentOperation" = "Getting item {0} of {1}" -f $counter,$total
        "PercentComplete"  = $percent
        "Completed"        = $percent -eq 100
      }
      Write-Progress @params
      $counter++
    }


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Tuesday, July 31, 2018 7:15 PM
    • Unproposed as answer by jrv Tuesday, July 31, 2018 7:15 PM
    • Proposed as answer by jrv Tuesday, July 31, 2018 7:15 PM
    • Unproposed as answer by jrv Tuesday, July 31, 2018 7:15 PM
    • Proposed as answer by jrv Tuesday, July 31, 2018 7:15 PM
    • Marked as answer by Richard MuellerMVP, Moderator Tuesday, August 7, 2018 1:00 PM
    Tuesday, July 31, 2018 7:02 PM
    Moderator
  • I made a few changes and the error went away but the progress starts and stays at 100% until it finishes copying... Stumped.. 

    $fileDL = Get-ChildItem -Path $Downloads
    $i=1
    $Downloads | %{
    [int]$percent = $i / $Downloads.count * 100
    Write-Progress -activity "Copying Downloads Folder  ... ($percent %)" -PercentComplete $percent -verbose
    Copy-Item $fileDL.fullname -Exclude $ExclusionList -Destination ($USBdrive + $user + "\" + "Downloads") -Recurse -container -Force
    $i++
    }


    Jason Ostrowski

    Tuesday, July 31, 2018 7:03 PM
  • $fileMD = Get-ChildItem -Path $MyDocuments -Recurse
    $i=1
    $totalcount = $fileMD.Count
    $fileMD | %{
        [int]$percent = $i / $totalcount * 100
        Write-Progress -activity "Copying MyDocuments Folder  ... ($percent %)" -PercentComplete $percent -verbose
        Copy-Item $_ -Destination ($USBdrive + $user + "\" + "MyDocuments") -Recurse -container -Force -Exclude $ExclusionList
        $i++
    }


    \_(ツ)_/

    Tuesday, July 31, 2018 7:08 PM
  • Stumped..

    I refer the honorable gentleman to my Write-Progress example posted some minutes ago.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 31, 2018 7:13 PM
    Moderator
  • JRV.. This corrected the progress meter but it doesn't copy anything now...

    Jason Ostrowski

    Tuesday, July 31, 2018 7:30 PM
  • Hi Bill

    Thank you for your suggestion... I'm trying to see how I can properly integrate it into my script... 


    Jason Ostrowski

    Tuesday, July 31, 2018 7:34 PM
  • Read the example carefully. Copy and paste it into a sample script. Run it and carefully observe its behavior.

    Once you understand how and why it works, you will better be able to update your existing script with correct, working code.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 31, 2018 7:42 PM
    Moderator