Executing another Powershell script
Published on: 16th Dec 2019
Updated on: 16th Jan 2022
Explanation
To execute another Powershell script file within a Powershell script,
-
Method #1 - run the hard coded script file
.\test1.ps1
-
Method #2 - run the script file name stored in a variable where the script location can be decided at runtime.
$ps_file = "$PSScriptRoot\test1.ps1" & $ps_file
Use case
- If you are managing multiple servers and each has a slightly different setup, you may want to have different script files for different servers.
- If you are managing multiple applications that require sophisticated maintenance scripts.
- Some scripts were developed by third parties and you don't want to merge their codes into yours.
Jump to #POWERSHELL blog
Author
Lau Hon Wan, software developer.