Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run multiple Powershell Scripts at the same time?

Tags:

powershell

so I have two .ps1 scripts that check for new Tasks every 60 seconds and I want to run them at the same time. I use them every day and starting them gets a little annoying. Ideally, I want to write a script that I can run that runs those two for me.

The Problem is that as they don't stop at some point, I can't just start them one after the other and I can't find a command to open a new PS ISE instance. However, one instance can't run both scripts at the same time. The Ctrl+T Option would be perfect, but I can't find the equivalent command.

Does anyone have an idea on how to solve this? Thanks!

like image 585
M.T. Avatar asked Dec 19 '22 18:12

M.T.


2 Answers

I think what you want is something like

Start-Process Powershell.exe -Argumentlist "-file C:\myscript.ps1"
Start-Process Powershell.exe -Argumentlist "-file C:\myscript2.ps1"

In addition to that: Use Import-Module $Modulepath inside the scripts to ensure the availability of the modules.

like image 120
restless1987 Avatar answered Mar 07 '23 12:03

restless1987


If you have Powershell 3.0+ you could use workflows. they are similar to functions, but have the advantage that you can run commands parallel.

so your workflow would be something like this:

workflow RunScripts {
    parallel {
        InlineScript { C:\myscript.ps1 }   
        InlineScript { C:\myotherscript.ps1 }
    }
}

keep in mind that a workflow behaves like a function. so it needs to be loaded into the cache first, and then called by running the RunScripts command.

more information: https://blogs.technet.microsoft.com/heyscriptingguy/2013/01/09/powershell-workflows-nesting/

like image 28
Balthazar Avatar answered Mar 07 '23 11:03

Balthazar