title: Scheduled Powershell Task, stuck Running
body: <p>I have problems when running a powershell script in Scheduled Tasks. It gets stuck in Running, even though the Transcript has logged out the last row "Done!" and it also looks like it has done exactly what i want it to. What am I missing?</p>
  
  <p>When running it from <code>Run</code> in Windows it also seems fine: <code>powershell -file "D:\_temp\copy_bat\copy_one_reprint_folder.ps1"</code></p>
  
  <p>Seetings in Task Scheduler is:</p>
  
  <ul>
  <li>Run with full priviliges</li>
  <li>Run weither user is logged on or not</li>
  <li>Action, start program <code>powershell.exe</code>
  
  <ul>
  <li><code>-file "D:\_temp\copy_bat\copy_one_reprint_folder.ps1"</code></li>
  </ul></li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/j3hJV.png" alt="Stuck in Running">
  <img src="https://i.stack.imgur.com/7204u.png" alt="History in TS"></p>
  
  <p>Please see code below if needed.</p>
  
  <pre><code># PowerShell RePRINT copy first folder, sorted on lastModified
  function timestamp
  {
      $ts = Get-Date -format s
      return $ts
  }
  
  $fromDirectory = "D:\_temp\copy_bat"
  $toDirectory = "D:\_temp\in_dummy"
  $extractGUIDdir = ""
  $docTypeDir = ""
  
  # Logging
  #########
  $ErrorActionPreference="SilentlyContinue"
  Stop-Transcript | out-null
  $ErrorActionPreference = "Continue"
  Start-Transcript -path $fromDirectory\copy_one_reprint_folder.log.txt -append
  ###########
  
  
  Write-Host ""
  Write-Host (timestamp) "Copy RePRINT extract started"
  Write-Host (timestamp) "============================"
  
  Get-ChildItem -path $fromDirectory | ?{ $_.PSIsContainer } | Sort-Object CreationTime | `
      Where-Object {$_.name -ne "_copied"} | `
      Select-Object -first 1 | `
          Foreach-Object{
              Write-Host (timestamp) $_.name
              $extractGUIDdir = $_.FullName
              Get-ChildItem -path $extractGUIDdir |  ?{ $_.PSIsContainer } | Where-Object {$_.Name -match "Purchase Order \(-999999997\)" -or $_.Name -match "Logistics documents \(-1000000000\)" -or $_.Name -match "Specifications \(-999999998\)"} | `
                  Foreach-Object{
                      Write-Host (timestamp) "  " $_.Name
                  }
              Write-Host ""
  
              Write-Host "These folders (document types), were also found but will not be included"
              Get-ChildItem -path $extractGUIDdir -Exclude "Logistics documents (-1000000000)", "Purchase Order (-999999997)", "Specifications (-999999998)" | ?{ $_.PSIsContainer } | `
                  Foreach-Object{
                      Write-Host (timestamp) " - " $_.name
                  }
              Write-Host ""
  
              Get-ChildItem -path $extractGUIDdir | ?{ $_.PSIsContainer } | Where-Object {$_.Name -match "Purchase Order \(-999999997\)" -or $_.Name -match "Logistics documents \(-1000000000\)" -or $_.Name -match "Specifications \(-999999998\)"} | `
                  Foreach-Object{
                          $temp_name = $_.FullName
                          Write-Host (timestamp) "copying files from " $_.FullName
                          Write-Host (timestamp) "                    to "  $toDirectory
                          #Copy-Item ($_.FullName)\*.* $toDirectory
                          Write-Host (timestamp) " copying meta-files..."
                          Copy-Item $temp_name\*.meta $toDirectory -Filter *.meta
                          Write-Host (timestamp) " copying pdf-files..."
                          Copy-Item $temp_name\*.pdf $toDirectory -Filter *.pdf
                          if(Test-Path $temp_name\*.* -Exclude *.meta, *.pdf)
                          {
                              Write-Host (timestamp) " WARNING/ERROR not all documents have been moved. Only PDFs was moved!"
                              Write-Host (timestamp) " Check folder for other document-types."
                          }
                  }
                  Move-Item $extractGUIDdir $fromDirectory\_copied
          }
  
  Write-Host (timestamp) " DONE!"
  
  # Stop logging
  Stop-Transcript
  </code></pre>