Background Job in Powershell

Question:

I’m trying to run a job in a background which is a .exe with parameters and the destination has spaces. For example:

and I want to run this with parameters:

I know that Start-Job does this but I’ve tried tons of different combinations and it either gives me an error because of the white space or because of the parameters. Can someone help me out with the syntax here? I need to assume that $exec is the path of the executable because it is part of a configuration file and could change later on.

Answer:

One way to do this is use a script block with a param block.

If there is a single argument with a space in it such as a file/folder path it should be quoted to treat it as a single item. The arguments are an array passed to the script block.

This example uses a script block but you can also use a PowerShell script using the -FilePath parameter of the Start-Job cmdlet instead of the -ScriptBlock parameter.

Here is another example that has arguments with spaces:


Here is an example using the $args built-in variable instead of the param block.

Source:

Background Job in Powershell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply