# Execute command in multiple directories at once

**URL:** <https://forums.powershell.org/t/execute-command-in-multiple-directories-at-once/16207>\
**Category:** PowerShell Help\
**Created:** [April 8, 2021, 9:26am UTC](https://forums.powershell.org/t/execute-command-in-multiple-directories-at-once/16207 "2021-04-08T09:26:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lilyntalmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/lilyntalmers/32/1165_2.png) [@lilyntalmers](https://forums.powershell.org/u/lilyntalmers)\
**Post date:** [April 8, 2021, 9:26am UTC](https://forums.powershell.org/t/execute-command-in-multiple-directories-at-once/16207/1 "2021-04-08T09:26:20Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [April 8, 2021, 3:59pm UTC](https://forums.powershell.org/t/execute-command-in-multiple-directories-at-once/16207/2 "2021-04-08T15:59:31Z")

</div>

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:

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![lilyntalmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/lilyntalmers/32/1165_2.png) [@lilyntalmers](https://forums.powershell.org/u/lilyntalmers)\
**Post date:** [April 9, 2021, 6:36am UTC](https://forums.powershell.org/t/execute-command-in-multiple-directories-at-once/16207/3 "2021-04-09T06:36:44Z")

</div>

> [@rob-simmers](#):
>
> ```auto
> #Get folder childtren in path, Alias for dir
> foreach ($path in (Get-ChildItem -Path C:\Scripts -Directory)) {
> $path.FullName
> #Change context to path, Alias for cd (Change Directory)
> Set-Location -Path $path.FullName
> }
> 
> ```

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

```auto
.\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.

---

<div class="post-metadata">

**Author:** ![lilyntalmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/lilyntalmers/32/1165_2.png) [@lilyntalmers](https://forums.powershell.org/u/lilyntalmers)\
**Post date:** [April 9, 2021, 11:38am UTC](https://forums.powershell.org/t/execute-command-in-multiple-directories-at-once/16207/4 "2021-04-09T11:38:13Z")

</div>

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

```auto
$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
}

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:26pm UTC](https://forums.powershell.org/t/execute-command-in-multiple-directories-at-once/16207/5 "2024-05-16T20:26:49Z")

</div>


