19

I have 6+ scripts and growing to run on a folder, what is the best way to have them run one after the other.

I have seen and tried this thread - it did not work unfortunately.

How to run multiple Scripts one by one in a powershell script

In a master .ps file - I have put links to the power shell scripts that need to be run in order

Run Scripts in order

'C:\Users\WP\Desktop\Scripts\1.ps1'
'C:\Users\WP\Desktop\Scripts\2.ps1'
'C:\Users\WP\Desktop\Scripts\3.ps1'

etc

This did not work either. Your help is appreciated - I have searched all over and can't seem to fix this issue.

Revised: I believe I will have to wait for each power shell script to finish before running the next one - as I have had errors when I tried to run 3 scripts one after the other - nothing happened when the scripts were run

Final -

I thank you all for your help - to keep things simple this is what I have done

My folder has the scripts below - I have then created a Master.ps1 with the code below inside it:

&"$PSScriptroot\1.ps1"
&"$PSScriptroot\2.ps1"
&"$PSScriptroot\3.ps1"
&"$PSScriptroot\4.ps1"
&"$PSScriptroot\5.ps1"
&"$PSScriptroot\6.ps1"

I have then run the Master.ps1 on the folder with all the files - and it does the job so far.

2
  • When you say one after the other, are you wanting to wait for each script to complete before running the next, or run them all at the same time? Commented Mar 9, 2016 at 21:50
  • Hi, I should wait for each one to finish - that makes sense. Run script 1 - then 2 then 3, sequentially :) Commented Mar 9, 2016 at 21:53

6 Answers 6

17

If you don't want to hard code the path, you can make Master.ps1 like this:

&"$PSScriptroot\1.ps1"
&"$PSScriptroot\2.ps1"
&"$PSScriptroot\3.ps1"

And it will look for those scripts in the same directory where it is.

Sign up to request clarification or add additional context in comments.

4 Comments

Has $PSScriptroot always been there? I've been parsing the script root with Split-Path $MyInvocation.MyCommand.Definition for what seems like forever.
Hi Mj - I did this &"C:\Users\WP\Desktop\Mjolinor\1.ps1" &"C:\Users\WP\Desktop\Mjolinor\2.ps1" - it worked :) - is there any chance - that it can wait to finish - before the next script runs - just in case in the future - I may have a long script , to prevent spaghetti results in the text files - thank you
They will run sequentially by default. You have to use jobs or runspaces to get it to multi-thread.
@TheMadTechnician - $PSScriptRoot appears to have shown up in V3.
3

Simply create a text file, using any code editor or text editor, and use the following example batch script:

start /min powershell.exe C:\your folder\script1.ps1
start /min powershell.exe C:\your folder\script2.ps1

Save it as a script.bat and open it. This will make two powershell scripts run at the same time.

Comments

2

To get the path that your script is in you can do this:

$MyInvocation.MyCommand.Definition

That will show something like 'C:\Users\WP\Desktop\Scripts\Master.ps1'. From that we can do a Split-Path to get just the folder, and run Get-ChildItem on the folder to get a list of files. We'll probably want to exclude the master script, so that we don't end up in a recursive loop, so that would look something like:

$ScriptPath = Split-Path $MyInvocation.MyCommand.Definition
Get-ChildItem "$ScriptPath\*.ps1" | Where{$_.FullName -ne $MyInvocation.MyCommand.Definition}

Then we just run those through a ForEach-Object loop, and invoke the script with the call operator & as such:

$ScriptPath = Split-Path $MyInvocation.MyCommand.Definition
Get-ChildItem "$ScriptPath\*.ps1" | Where{$_.FullName -ne $MyInvocation.MyCommand.Definition} | ForEach-Object { & $_.FullName }

Edit: Hm, that wasn't filtering right. Here's a re-write that does filter out the master script correctly.

$Master = Split-Path $MyInvocation.MyCommand.Definition -Leaf
$ScriptPath = Split-Path $MyInvocation.MyCommand.Definition
Get-ChildItem "$ScriptPath\*.ps1" | Where{$_.Name -ne $Master} | ForEach-Object { & $_.FullName }

3 Comments

TMD, thank you - In the folder i put 2 scripts and the master script that calls those ...something happened .. but it went into a loop after. In the master I put this : $MyInvocation.MyCommand.Definition $ScriptPath = Split-Path $MyInvocation.MyCommand.Definition Get-ChildItem "$ScriptPath\*.ps1" | Where{$_.FullName -ne $MyInvocation.MyCommand.Definition} | ForEach-Object { & $_.FullName } - I am not sure if I did it right?
Sorry, my first version wasn't filtering out the master script, so it was running itself as part of the script, and that just created an endless loop. I updated my answer.
Thank you I will add this to my power shell tools - box - :)
1

Are you hard coding the paths to the files in the master file?

In this case something like this should work

Invoke-Expression "C:\Users\WP\Desktop\Scripts\1.ps1"
Invoke-Expression "C:\Users\WP\Desktop\Scripts\2.ps1"
Invoke-Expression "C:\Users\WP\Desktop\Scripts\3.ps1"

1 Comment

I would prefer not to hard code - but as a powershell noob - I have no clue. If I put a master.ps in the folder with all the other scripts - how would i call them from the master? Thank you Captain
1

The reason why the powershell screen pops up is cuz you dont have the argument -NoExit set. Please use the following as an example to see if your code is running properly:

Start-Process "$pshome\powershell.exe" -ArgumentList "-NoExit", "-Command '& script'" -wait

Sorry for the late response but I also ran into the issue and thats how I was able to resolve it to make sure it was work.

Also I changes the file names as follows:

$Scripts =
@(
 ".\C:\Scripts\First.ps1"
 ".\C:\Scripts\Second.ps1"
 ".\C:\Scripts\Third.ps1"
 );

Comments

0

To run multiple scripts sequentially you can use the -Wait parameter on Start-Process like so:

$scriptsList = 
@(
    'C:\Users\WP\Desktop\Scripts\1.ps1'
    'C:\Users\WP\Desktop\Scripts\2.ps1'
    'C:\Users\WP\Desktop\Scripts\3.ps1'
)

foreach($script in $scriptsList)
{
    Start-Process -FilePath "$PSHOME\powershell.exe" -ArgumentList "-command '& $script'" -Wait
}

PowerShell will wait for the current script to finish before running the next script

1 Comment

Hi BC - I ran this the power shell screens came up - but no changes happened in the text files - I don't know why :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.