title: AWS Ec2 PowerShell userdata script extremely slow on boot
body: <p>I'm currently starting a "Spot Instance" on the g2.xlarge CPU, and passing a startup script with the userdata paremeter Amazon has made available.</p>
  
  <p>I want to move some files upon startup, from the root volume to the EBS volume. This is all perfect, and I have written a Powershell script to do this which runs just fine when I run the .ps1 file in a Remote Desktop Connected session from my mac. It takes just about 30 seconds to complete moving these files.</p>
  
  <p>However!!! :) When I pass that EXACT SAME PowerShell script into the User-data upon creating the Spot Instance, it looks like it kinda freezes (it doesn't though, it's just slow) and it takes a whopping <strong>10-20 minutes to finish</strong> moving the files! :/</p>
  
  <p>Which means that the whole boot process is taking forever.</p>
  
  <p>I have tried moving the files with robocopy, standard move commands and even the Powershell native Move-Item in a foreach loop. Same applies though: All the commands work just fine when being run from an RDP session, but when passed through the userdata upon launching the instance, to be run at boot, they are extremely slow to finish (or start?) :/</p>
  
  <p><strong>Does anyone know what the issue could be?</strong></p>
  
  <p>I really need the server to move the files automatically, and without having to RDP into the server to do it.</p>
  
  <p>Thanks! :)</p>
  
  <p>btw I'm running Windows Server 2012 R2</p>
  
  <p>EDIT:
  In case it has something to do with the script I use to move the files, here it is. :)</p>
  
  <pre><code>Start-Job -Name MoveFolder -ScriptBlock {
  $path = "C:\Program Files (x86)\OldDestination"
  $archpath = "Z:\NewDestination"
  
  $counter = 0
  $oldpercentage = 0
  $files = Get-Childitem -Path $path -recurse -force
  $totalcount = $files.count
  
  $lasttime = [int][double]::Parse((Get-Date -UFormat "%s"))
  foreach($file in $files) {
    $filename = $file.FullName
    Move-Item $file.FullName -destination $archpath -force -ErrorAction:SilentlyContinue
    $counter++
    $percentage = ($counter/$totalcount)*100
    $percentage = [math]::Round($percentage)
    if($percentage -gt 99) { $percentage = 99 }
    $runtime = [int][double]::Parse((Get-Date -UFormat "%s"))
    $runtime = $runtime - $lasttime
  
    if($runtime -gt 5) { //Max. log process on server every &gt; 5 seconds
      if($percentage -gt $oldpercentage) {
        Invoke-WebRequest -Uri http://exampleurl.com/$percentage -Method GET -UseBasicParsing
        $lasttime = [int][double]::Parse((Get-Date -UFormat "%s"))
        $oldpercentage = $percentage
      }
    }
  }
  $percentage = 100
  Invoke-WebRequest -Uri http://exampleurl.com/$percentage -Method GET -UseBasicParsing
  }
  Wait-Job -Name MoveFolder
  </code></pre>