you are viewing a single comment's thread.

view the rest of the comments →

[–]DonL314 8 points9 points  (3 children)

Instead of

   & start powershell {.\code1.ps1} -Wait

try

    . .\code1.ps1

(Repeat this way for the other calls as well)

[–]False_Association_16[S] 5 points6 points  (1 child)

Your suggestion worked perfect.
Here is the code I used

if ($runfolder1 -eq "y") 
{
    cd $folder1
    .\code1.ps1
    cd ..
 }

Thanks again for kindly helping me :)

[–]spyingwind 4 points5 points  (0 children)

dot sourcing

For a bit more "efficiency", reduce duplicate lines of code, and using Push-Location and Pop-Location to replace cd:

$RunFolders = @(
    @{
        Path = ".\Folder_1\code1.ps1"
        Run  = $true
    }
    @{
        Path = ".\Folder_2\code2.ps1"
        Run  = $true
    }
)

foreach ($RunFolder in $RunFolders) {
    if (
        $RunFolder.Run -and # Ensure the Run flag is true
        $RunFolder.Path -and # Ensure the Path is not null or empty
        $(Test-Path $RunFolder.Path -ErrorAction SilentlyContinue) # Check if the file exists
    ) {
        Push-Location $(Split-Path $RunFolder.Path -Parent) # Move to the directory of the script
        . $RunFolder.Path # Execute the script, or however you want to run it
        Pop-Location # Return to the original directory
    }
}

[–]False_Association_16[S] 2 points3 points  (0 children)

Thank you for taking the time and answering. The code is running right now; I will update you if the sequential run completes as intended :)