title: Looking for ways to speed up this Powershell script
body: <p>I've talked to Microsoft, they have said that they throttle the interface on their side but that if you run 7 jobs with 10,000 each, rather then one with 70,000, it should work fine because they only throttle each session individually.</p>
  
  <p>I've streamlined the script itself using <code>Measure-Command</code> to get each section working at max speed, and got it working exactly how I want. 
  So we're back to the original question now, somehow split the CSV input and run import jobs on each chunk, and also possibly do the same with the deleting and have multiple delete loops running simultaneously. </p>
  
  <p>The two loops that need to be "multicasted" first, then the full updated script below:</p>
  
  <p>Importing from CSV:</p>
  
  <pre><code>Do {
      $counter2 = 0
      $CSVpath = "C:\temp\testgal.csv"
      $contacts = Import-Csv "$CSVpath" 
      Measure-Command{
          foreach ($c in $contacts)  {
              Try {
                  $ErrorActionPreference = "stop"
                  $counter2++
                  $name = $c.displayName
                  $rawProxy = $c.proxyAddresses
                  $proxysplit = $rawproxy -split '(?&lt;!\\);'
                  $proxyquoted = $proxysplit.replace('x500','"x500').replace('x400','"x400').replace('X500','"X500').replace('X400','"X400')
                  $proxy = $proxyquoted
                  Write-Progress -Activity 'Creating Contacts...' -CurrentOperation $c.displayName -PercentComplete (($counter2 / $contacts.Count) * 100)
                  New-MailContact -ExternalEmailAddress $c.Mail -Name "`"$name`"" -Alias $c.mailNickname -DisplayName $name -FirstName $c.givenName -Initials $c.initials -LastName $c.sn
                  Set-MailContact -Identity $c.mailNickname -CustomAttribute1 "CreatedWithScript" -CustomAttribute3 $c.extensionAttribute3 -EmailAddresses $proxy
                  Set-Contact -Identity $c.mailNickname -City $c.l -Company $c.company -Department $c.department -Office $c.physicalDeliveryOfficeName `
                      -Phone $c.telephoneNumber -PostalCode $c.postalCode -Title $c.title
              }Catch {$_ | Out-File 'C:\temp\Error_Log.txt' -Append
                      Write-Warning -message "$name Already exists."}
          }
      }
  Write-Progress -Activity "Creating Contacts..." -Status "Completed." -Completed
  Write-Host "----------------------------------"
  Write-Host "All Contacts successfully created."
  Write-Host "----------------------------------"
  $ErrorActionPreference = "continue"    
  }
  Until($? -eq "True")
  </code></pre>
  
  <p>Removing from 365:</p>
  
  <pre><code>Do{
  $totalnumberremaining = Get-Mailcontact -Filter "CustomAttribute1 -eq 'CreatedWithScript'" -ResultSize unlimited
  $numberremaining = Get-Mailcontact -Filter "CustomAttribute1 -eq 'CreatedWithScript'" -ResultSize 1000
  $counter = 0
  Do{
      foreach ($contact in $numberremaining) {
          $counter++
          Remove-MailContact -Identity $contact.Alias -Confirm:$False
      }
      Write-Progress -Activity 'Deleting Contacts...' -CurrentOperation $contact -PercentComplete (($counter / $totalnumberremaining.count) * 100)
  }
  Until($numberremaining -eq $null)
  </code></pre>
  
  <p>Full script:</p>
  
  <pre><code>#Logs into Office365
  Do {
      $UserCredential = Get-Credential
      $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
      Import-PSSession $session
      Connect-MsolService -cred $UserCredential
  }
  Until ($? -eq "True")
  
  Do{
  #Removes all the current Contacts
  $totalnumberremaining = Get-Mailcontact -Filter "CustomAttribute1 -eq 'CreatedWithScript'" -ResultSize unlimited
  $numberremaining = Get-Mailcontact -Filter "CustomAttribute1 -eq 'CreatedWithScript'" -ResultSize 1000
  $counter = 0
  Do{
      foreach ($contact in $numberremaining) {
          $counter++
          Remove-MailContact -Identity $contact.Alias -Confirm:$False
      }
      Write-Progress -Activity 'Deleting Contacts...' -CurrentOperation $contact -PercentComplete (($counter / $totalnumberremaining.count) * 100)
  }
  Until($numberremaining -eq $null)
  Write-Progress -Activity 'Deleting Contacts...' -Status "Completed." -Completed
  Write-Host "----------------------------------"
  Write-Host "All Contacts successfully removed."
  Write-Host "----------------------------------"
  Pause
  
  # Imports new contacts from the CSV
  Do {
      $counter2 = 0
      $CSVpath = "C:\temp\testgal.csv"
      $contacts = Import-Csv "$CSVpath" 
      Measure-Command{
          foreach ($c in $contacts)  {
              Try {
                  $ErrorActionPreference = "stop"
                  $counter2++
                  $name = $c.displayName
                  $rawProxy = $c.proxyAddresses
                  $proxysplit = $rawproxy -split '(?&lt;!\\);'
                  $proxyquoted = $proxysplit.replace('x500','"x500').replace('x400','"x400').replace('X500','"X500').replace('X400','"X400')
                  $proxy = $proxyquoted
                  Write-Progress -Activity 'Creating Contacts...' -CurrentOperation $c.displayName -PercentComplete (($counter2 / $contacts.Count) * 100)
                  New-MailContact -ExternalEmailAddress $c.Mail -Name "`"$name`"" -Alias $c.mailNickname -DisplayName $name -FirstName $c.givenName -Initials $c.initials -LastName $c.sn
                  Set-MailContact -Identity $c.mailNickname -CustomAttribute1 "CreatedWithScript" -CustomAttribute3 $c.extensionAttribute3 -EmailAddresses $proxy
                  Set-Contact -Identity $c.mailNickname -City $c.l -Company $c.company -Department $c.department -Office $c.physicalDeliveryOfficeName `
                      -Phone $c.telephoneNumber -PostalCode $c.postalCode -Title $c.title
              }Catch {$_ | Out-File 'C:\temp\Error_Log.txt' -Append
                      Write-Warning -message "$name Already exists."}
          }
      }
  Write-Progress -Activity "Creating Contacts..." -Status "Completed." -Completed
  Write-Host "----------------------------------"
  Write-Host "All Contacts successfully created."
  Write-Host "----------------------------------"
  $ErrorActionPreference = "continue"    
  }
  Until($? -eq "True")
  
  # Exits or repeats script on user imput
  $response = read-host "Repeat(R) or Quit(Q)?"
  }
  While ($response -ne "Q")
  Remove-PSSession $session
  </code></pre>