Launching PowerShell Script (PS1) Without Waiting to Finish

Hey Guys -

I’ve got what will hopefully be a simple question for most. I’m trying to find the best way to launch a PS1 from the command line in a method where the application that called the execution doesn’t wait for it to complete.

Explanation

I’ve got an application that imports media files on a regular basis. I have it configured so that after each file is imported, a PowerShell script is executed which initiates an exe with a few passed variables that scans the imported file into a library plus writes some logging. It works great except for one thing: before additional files can be imported, the app waits for the script initiated by the prior file to complete which takes a couple of minutes. Therefore, importing ~5 files the way I have it set up now takes about 15 minutes yet if it didn’t wait for the script to complete after each, it would take about 15 seconds.

Current Configuration

Upon a file being imported into the app, I have the path “C:\Windows\SysWOW64\WindowsPowerShell\v1.0\PowerShell.exe” executed with the arguments "-ExecutionPolicy Bypass -file “C:\Scripts\Update.ps1”. I was hoping it would be as easy as adding a parameter like “-nowait”, but guess its not that easy so I tried making the adjustment to the string causing the delay within the script.

The Script

The script does a few things, but it’s the string that launches the exe which takes so long. I tried adding “Start-Job” to the beginning of the string, but it didn’t work since it started with an “&”. The string is “& “$scanner” --scan --refresh --section $id --directory $path” where $scanner is a variable set to an EXE file and other variables are pulled from the importing app.

Any suggestions? Thank You!

The app would have to invoke the PowerShell job on a separate thread, or interface directly with the C# APIs in the PowerShell Standard Library in order to have more control over how it executes and how data is returned, I think.

Even if you get a Start-Job command to work (which you can, for sure), if the parent PowerShell process ends, it will terminate any remaining jobs as it exits.