Skip to content

Instantly share code, notes, and snippets.

@DCCoder90
Created May 15, 2021 00:45
Show Gist options
  • Save DCCoder90/dc05ff304becd7e98d36950ebfc4cb1f to your computer and use it in GitHub Desktop.
Save DCCoder90/dc05ff304becd7e98d36950ebfc4cb1f to your computer and use it in GitHub Desktop.
Powershell scrip to iterate through a directory containing git repos and perform a specified action. https://stackoverflow.com/questions/66249041/iterate-thru-all-folders-at-the-current-path-where-the-script-resides
function global:Start-ForAllRepos
{
[CmdletBinding()]
param (
[Parameter(Position=0)]
[string]
$Cmd = 'git status',
[Parameter(Position=1)]
[string[]]
$RepoRoot = @(
'd:\git'
),
[switch]
$Wait
)
$currentLocation = Get-Location
foreach ($root in $RepoRoot)
{
Set-Location $root
Get-ChildItem -Directory -Exclude '.vscode' |
Select-Object -ExpandProperty FullName |
ForEach-Object {
# check if this is a git repo
if (-not (Test-Path (Join-Path $_ '.git')))
{
#"Not a git repo $($_)"
continue;
}
"-"*72
$_
$Cmd
"-"*72
Set-Location $_
cmd.exe /c $Cmd
# wait when requested
if ($Wait) {
$null = Read-Host -Prompt "Press enter to continue..."
}
}
Set-Location $root
}
Set-Location $currentLocation
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment