Created
December 21, 2023 20:30
-
-
Save joao-pedrozo/b0baf28aba5bba802bf54862e2bc8984 to your computer and use it in GitHub Desktop.
make all github repositories private using with powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set your GitHub username and access token | |
$githubUsername = "abc" | |
$githubToken = "YOUR_TOKEN" | |
# Get repositories | |
$repos = Invoke-RestMethod -Uri "https://api.github.com/users/$githubUsername/repos" -Method Get | |
# Loop through each repository and update it | |
foreach ($repo in $repos) { | |
$repoName = $repo.name | |
# Check if the repository is a fork | |
if ($repo.fork) { | |
Write-Host "Skipping fork: $($repoName)" | |
continue | |
} | |
# Check if the repository is already private | |
if ($repo.private) { | |
Write-Host "Repository $($repoName) is already private." | |
continue | |
} | |
$uri = "https://api.github.com/repos/$githubUsername/$repoName" | |
# Use Invoke-RestMethod to send PATCH request | |
try { | |
Invoke-RestMethod -Uri $uri -Method Patch -Headers @{ | |
Authorization = "Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($githubUsername):$($githubToken)")) | |
"Content-Type" = "application/json" | |
} -Body '{"private": "true"}' | |
Write-Host "Repository $($repoName) updated to private." | |
} catch { | |
Write-Host "Error updating repository $($repoName): $_" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment