user3302439
user3302439

Reputation: 11

Powershell Batch file to execute .ps1 files in directories

I have a very good deployment method.

I currently execute .sql files using powershell and I write results out to a log file. Currently the batch file, .ps1 file and the .sql files are all in the same directory.

I would like to have one batch file that executes Build 01 .ps1 and when Build 01 .ps1 finishes it executes Build 02 .ps1 etc.

Sometimes I have 20 build folders to deploy and I am trying to avoid having to double click 20 .bat files.

I am new to PowerShell - I apologize if this is not clear please ask. Thank you.

Upvotes: 1

Views: 2871

Answers (2)

TheMadTechnician
TheMadTechnician

Reputation: 36332

Or, if you strictly want a batch file instead of a script you could do something like:

Start "" /wait "powershell.exe . .\build01.ps1"
Start "" /wait "powershell.exe . .\build02.ps1"
Start "" /wait "powershell.exe . .\build03.ps1"
Start "" /wait "powershell.exe . .\build04.ps1"

To clarify, I am referring to a batch file as a file ending with the .BAT or .CMD extension. The above should run the .PS1 scripts in order, waiting for each to finish before launching the next one. If you would prefer to call each batch file on its own from an additional batch file you would use the CALL command, such as:

CALL Batch01.bat
CALL Batch02.bat
CALL Batch03.bat

Upvotes: 0

Steve B
Steve B

Reputation: 37710

Try this:

Get-ChildItem -Recurse -include "*.ps1" | % { & $_ }

To understand how it works, you can decompose this line into:

$allScripts = Get-ChildItem -Recurse -include "*.ps1" #Get all ps1 file under the current directory
foreach($script in $allScripts){
    & $script # run the script
}

A side note, the current directory is the directory where you ran the command. This is not the script directory itself (if you run the command from a script)

Upvotes: 1

Related Questions