wp44
wp44

Reputation: 265

Run Multiple Powershell Scripts Sequentially - on a Folder - Combine Scripts into a Master Script

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.

Upvotes: 19

Views: 81652

Answers (6)

Kristián Jalovec
Kristián Jalovec

Reputation: 31

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.

Upvotes: 3

InvalidFileName
InvalidFileName

Reputation: 11

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"
 );

Upvotes: 1

Bluecakes
Bluecakes

Reputation: 2149

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

Upvotes: 0

TheMadTechnician
TheMadTechnician

Reputation: 36277

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 }

Upvotes: 2

mjolinor
mjolinor

Reputation: 68243

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.

Upvotes: 17

thecaptain0220
thecaptain0220

Reputation: 2168

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"

Upvotes: 1

Related Questions