0
votes

I have this for my code:

get-childitem -Path "location1" -Recurse | where-object {$_.LastWriteTime -lt (get-date).AddDays(-365)} | move-item -destination "NewLocation1"

get-childitem -Path "Location2" -Recurse | where-object {$_.LastWriteTime -lt (get-date).AddDays(-365)} | move-item -destination "NewLocation2"

get-childitem -Path "Location3" -Recurse | where-object {$_.LastWriteTime -lt (get-date).AddDays(-365)} | move-item -destination "NewLocation3"

Real basic question. Does powershell run each task separate are all at once? Will the move from Location2 to NewLocation2 happen after the move to NewLocation1 has finished or does all of this run at once?

2

2 Answers

0
votes

why not do it in one task? Something like this

  [PSObject[]]$Myarray = New-Object PSObject -Property @{ Location="C:\temp"; Destination="C:\tmp4"}
  $Myarray += New-Object PSObject -Property @{ Location="C:\temp2"; Destination="C:\tmp5"}
  $Myarray += New-Object PSObject -Property @{ Location="C:\temp3"; Destination="C:\tmp6"}

  $Myarray | %{get-childitem -Path ($_.Location) -Recurse | where-object {$_.LastWriteTime -lt (get-date).AddDays(-365)} | move-item -destination $_.Destination -Force}

if you want run in multi task you can do it

  [PSObject[]]$Myarray = New-Object PSObject -Property @{ Location="C:\temp"; Destination="C:\tmp4"}
  $Myarray += New-Object PSObject -Property @{ Location="C:\temp2"; Destination="C:\tmp5"}
  $Myarray += New-Object PSObject -Property @{ Location="C:\temp3"; Destination="C:\tmp6"}

  $Myarray | %{Start-Job -ScriptBlock {get-childitem -Path ($_.Location) -Recurse | where-object {$_.LastWriteTime -lt (get-date).AddDays(-365)} | move-item -destination $_.Destination -Force}}

you can see state then like this

  Get-Job
0
votes

To answer your real basic question: Each task will run sequentially in your current script.

To have the commands run concurrently, one option is to use Start-Job.