Execute command in multiple directories at once

There is a “Parent Folder”. Inside that parent folder I have GitPush.ps1, Child Folder 1, Child Folder 2, Child Folder 3.

I want to execute this command git add -A ; git commit -m "Auto Git Commit" ; git push to all Child Folders.

I am a long time linux user and I recently moved to windows. In linux I used to use this script:

for dir in $PWD/*/.git; do
        cd "${dir%.git}" # PE: remove .git substring
        git add -A ; git commit -m "Auto Git Commit" ; git push

It’s fairly similar in Powershell. Rather than changing the context of the directory, you can also just pass the path to git. The semi-colons are line breaks and are just executed as separate commands in Powershell as well. Powershell uses cmdlets, but there are aliases so “dir” is really Get-ChildItem:

PS C:\> Get-Alias cd

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           cd -> Set-Location

PS C:\> Get-Alias dir

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           dir -> Get-ChildItem

Here is a basic code to execute a loop:

#Get folder childtren in path, Alias for dir
foreach ($path in (Get-ChildItem -Path C:\Scripts -Directory)) {
    #Change context to path, Alias for cd (Change Directory)
    Set-Location -Path $path.FullName
1 Like

Thanks a lot for the help. But I’m getting this error:

.\up.ps1 : The term '.\up.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ .\up.ps1
+ ~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.\up.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Also, is there any way to bind git add -A ; git commit -m "Auto Git Commit" ; git push this code inside the script? So, it automatically execute the command when I execute the PowerShell script.

Thanks for the help @rob-simmers. But I found a solution on reddit and it worked for me.

$MainFolder = "The Parent Folder Location"

$Folders = Get-ChildItem -LiteralPath $MainFolder -Directory -Depth 1

ForEach ($Folder in $Folders) {
set-location $folder.fullname
git add -A ; git commit -m "Auto Git Commit" ; git push