Execute a GIT command in multiple folders using PowerShell

If you as me found yourself working with projects spanning over multiple folders you have probably struggled with do a git pull for all or some of the folders manually. Wouldn't it be great if you could perform a pull for all those folders at once? If you're on Windows you can of course do that using a PowerShell-script!

Let's say your project folder structure looks like this and that all folders are GIT repositories.
Folder structure

Note the git-multi.ps1 file below the folders, that script can actually issue a single GIT command over all the folders like this:

PS C:\Projects\InSurvey> .\git-multi.ps1 -cmd "pull"  

Which results in:

Pretty sweet? You can change the cmd flag to any GIT command supported by your version of GIT. Of course some commands will be hard to perform over multiple folders at once, like a commit or merge but for for some commands like pull or status it saves a lot of time!

The script looks like this:

param (

    # The root directory to perform the pull in
    $baseDir = ".",

    # How deep down you want to look for .git folders
    $depth = 2,

    # The command you want to perform
    $cmd = "status"

$gitFolderName = ".git"

function Go () {

    # Finds all .git folders by given path, the -match "h" parameter is for hidden folders 
    $gitFolders = Get-ChildItem -Path $baseDir -Depth $depth -Recurse -Force | Where-Object { $_.Mode -match "h" -and $_.FullName -like "*\$gitFolderName" }

    ForEach ($gitFolder in $gitFolders) {

        # Remove the ".git" folder from the path 
        $folder = $gitFolder.FullName -replace $gitFolderName, ""

        Write-Host "Performing git $cmd in folder: '$folder'..." -foregroundColor "green"

        # Go into the folder
        Push-Location $folder 

        # Perform the command within the folder
        & git $cmd

        # Go back to the original folder

function Main () {  

# Executes the main function


The script requires you to have git globally installed on your computer.

comments powered by Disqus