Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save noisecrime/558b696af624616fa5de14cb04497c21 to your computer and use it in GitHub Desktop.
Save noisecrime/558b696af624616fa5de14cb04497c21 to your computer and use it in GitHub Desktop.
Automate Add Local Repositories To GithubDesktop.
# ------------------------------------------------------------------------------------
# Automate Add Local Repositories To GithubDesktop
# ------------------------------------------------------------------------------------
# Version: 1.0.1
# Fixed Test-Path to use Literalpath for paths with spaces in.
# ------------------------------------------------------------------------------------
# Given a txt file that lists local repository paths add each in turn to Github Desktop.
# List of git paths are expected to point to the '.git' directory
# e.g.
# G:\Programming\SignedDistanceFields\.git
# K:\Repos\monday\.git
# D:\Repos_2025\monkey\tool\.git
# ------------------------------------------------------------------------------------
# DISCLAIMER
# ------------------------------------------------------------------------------------
# Use this script at your own risk.
# This script is provided ​“AS IS”. the Developer makes no other warranties,
# express or implied, and hereby disclaims all implied warranties.
# ------------------------------------------------------------------------------------
# INSTRUCTIONS - IMPORTANT
# ------------------------------------------------------------------------------------
# Obtain a list of git folders on your machine either by hand or using a
# Search Tool such as 'Everything' from https://www.voidtools.com
# using these parametets "folder: nopath:.git nopath:!.git. nopath:!vscode".
# Ensure that each repository directory path is on a new line.
#
# Run the script.
# Select the txt file that contains a list of .git folders.
# Wait for Github Desktop to open and load the repository.
# If Github Desktop asks to 'Add local repository' click 'Add repository' or 'cancel'
# Wait for Github Desktop to complete loading the repository.
# click in Powershell console and press 'enter/return' to move to the next repository.
# ------------------------------------------------------------------------------------
# Tip
# Use Set-ExecutionPolicy to fix 'cannot be loaded because running scripts is disabled on this system'
# Get-ExecutionPolicy -List
# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
# https://stackoverflow.com/questions/37554048/get-filename-from-openfiledialog-by-clicking-a-browse-button
Function Get-FileName($initialDirectory)
{
Add-Type -AssemblyName System.Windows.Forms
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.Title = "Please Select File"
$OpenFileDialog.InitialDirectory = $initialDirectory
# $OpenFileDialog.filter = "All files (*.*)| *.*"
# $OpenFileDialog.filter = "CSV (*.csv)| *.csv"
$OpenFileDialog.filter = "Text (*.txt)| *.txt"
# Out-Null supresses the "OK" after selecting the file.
$OpenFileDialog.ShowDialog() | Out-Null
return $OpenFileDialog.FileName
}
# Locate Local List of Repositories (txt) one per line.
$repoListPath = Get-FileName('C:\')
Write-Host "Opening repository DB: $repoListPath"
# Read all lines from the repo list file
$gitFolders = Get-Content -Path $repoListPath
foreach ($gitRoot in $gitFolders )
{
# Trim any whitespace
$gitRoot = $gitRoot.Trim()
# Skip empty lines
if ([string]::IsNullOrWhiteSpace($gitRoot))
{
continue
}
# Get the parent directory of the .git folder
$repoPath = Split-Path -Path $gitRoot -Parent
if (-not (Test-Path -LiteralPath $repoPath))
{
Write-Warning "Repository directory not found: $repoPath"
continue
}
Write-Host "Opening repository: $repoPath"
# Launch GitHub Desktop with the repo path
github `""$repoPath`""
if ($psISE)
{
# Prompt user for input - works in Powershell ISE
$input = Read-Host "Press [Enter] to continue or type 'q' to quit"
if ($input -eq 'q')
{
Write-Host "Cancel command received. Exiting script."
break
}
}
else
{
# Untested
# Wait for user input before continuing
Write-Host "Press any key to continue, or press [Esc] to cancel..."
# Wait for key press - this does not work in Powershell ISE!
$key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
# Check if the key is Escape
if ($key.VirtualKeyCode -eq 27)
{
Write-Host "Cancel key pressed. Exiting script."
break
}
}
}
@noisecrime
Copy link
Author

Additional Notes:
As of August 2025 you are still unable to batch or automate adding local repositories into Github Desktop, despite it being a frequent request for a decade. See desktop/desktop#1574

This Powershell script attempts to semi-automate the process, though requires you to obtain a list of paths to all your .git repository directories on your machine. It will loop through each repository in the text file and open them in turn, waiting for you to click the 'add repository' button in Github Desktop, before moving t the next repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment