[PowerShell] How to run background job using Start-Job cmdlet

2 minute read

Start-Job cmdlet starts a PowerShell background job on the local computer. This cmdlet could run one or more jobs in the background until those jobs are completed.

Below is a simple scenario when we use Start-Job cmdlet.

Scenario 1:

Given that I receive an email on a Product pricing list from Product department and the email contains an attachment file named productPricingAug.txt
When I save the productPricingAug.txt into my computer
And I run Job 1 to copy from a source path (where the productPricing.Aug.txt was saved from the email’s attachment) to a destination path (where productPricing.Aug.txt file will be processed)
And I run Job 2 to get the content of productPricing.Aug.txt
And I run Job 3 to filter the product which has the price between USD100 - USD 250 from productPricingAug.txt
And I run Job 4 to save the matched result in AugProductPriceResult.txt file
And I run Job 5 to delete the previous productPricingAug.txt
Then I will receive the result of the products’ price which range from $100 - $ 250 in productPricingAug.txt

As you could see, there are five jobs that need to be run accordingly. To simplify the process, we could use a PowerShell script to handle all of them. By putting those 4 jobs in the Start-Job cmdlet { }, we will process all the 4 jobs within the { } and when all the 4 jobs completed, then we could perform the last job which is deleting the previous file.

Example script:


$job = Start-Job -Name "GetProductPricingWithinUSD100-USD250" -ArgumentList $pwd -ScriptBlock {

$job = Start-Job -Name "GetProductPricingWithinUSD100-USD250" -ArgumentList $pwd -ScriptBlock {

    # Job 1 : Copy a file from source(where the productPricing.Aug.txt was saved from the email’s attachment) to destination path (where productPricing.Aug.txt file will be processed)
    $sourcePath = "C:\Users\Afidah\Downloads\productPricingAug.txt"
    $destinationPath = "C:\ProductListing\"
	
    Copy-Item $sourcePath -Destination $destinationPath

    $productPricingFile = "$($destinationPath)\productPricingAug.txt"
    $pattern = "[a-zA-Z]+(\d+)"

    $reportResultArr = @()

	# Job 2 : Get the content of ProductPriceListing.txt
    Get-Content $productPricingFile | ForEach-Object {
		$priceArr = $_.Split(";")
		$priceArr[1] -match $pattern | Out-Null
		   
		# Job 3 : Filter the product which has the price between USD100 - USD250
		if ($Matches[1] -ge 100 -and $Matches[1] -le 250)  {    
			$productpricingDataArr = $_
			$reportResultArr =  $reportResultArr +  $productpricingDataArr           
		}      
    }
	
	# Job 4 : Save the matched result in AugProductPriceResult.txt file 
    Set-Content  $reportResultArr  -Path "$destinationPath\AugProductPriceResult.txt "
}

# Wait on the job to finish
Wait-Job -Job $job | Out-Null

# Job 4 : Delete productPricingAug.txt from $destinationpath
Write-host $productPricingFile -ForegroundColor Yellow

Write-host "Before:" -ForegroundColor Yellow
Get-ChildItem -Path  $destinationPath 

# Write-host "Deleting $($productPricingFile)"  -ForegroundColor Yellow
Remove-Item -Path $productPricingFile

# Write-host $productPricingFile -ForegroundColor Green
Write-host "After:" -ForegroundColor Yellow
Get-ChildItem -Path  $destinationPath 

You may get the sample test data at productPricingAug.txt