Auto Update Git Aliases

Because I am lazy I wrote a script so I can update all my machines from the one location which is the gist below. This works in powershell core. Again this is mostly for me but feel free to use as needed.

Update your $profile to have th following environment variable set. This will allows the wid and pid aliases to work correctly.

$env:GIT_WORK_EMAIL = ""
$env:GIT_WORK_USERNAME = "myworknameoralias"

Script to update aliases. Add to your profile or a function in your profile that you can call when you want.

$gitConfig = Get-Content $env:userprofile\.gitconfig
$hasAliasInclude = $false
$aliasIncFile = "$env:userprofile\"

# Update inc. 
if((Test-Path $aliasIncFile)) {
    Remove-Item -Path $aliasIncFile -Force | Out-Null
Write-Host "Updating the Alias Include"
Invoke-WebRequest -UseBasicParsing -Uri "" -OutFile $aliasIncFile
(Get-Content $aliasIncFile) | Foreach-Object { $_ -replace "ENTERPERSONALEMAILADDRESS", $env:GIT_PERSONAL_EMAIL } | Set-Content $aliasIncFile
(Get-Content $aliasIncFile) | Foreach-Object { $_ -replace "ENTERPERSONALUSERNAME", $env:GIT_PERSONAL_USERNAME } | Set-Content $aliasIncFile
(Get-Content $aliasIncFile) | Foreach-Object { $_ -replace "ENTERWORKEMAILADDRESS", $env:GIT_WORK_EMAIL } | Set-Content $aliasIncFile
(Get-Content $aliasIncFile) | Foreach-Object { $_ -replace "ENTERWORKUSERNAME", $env:GIT_WORK_USERNAME } | Set-Content $aliasIncFile

for ($i = 0; $i -lt $gitConfig.Count; $i++) {
    if ($gitConfig[$i].Contains("[include]") -and $gitConfig[$i+1].Contains("path =")) {
        Write-Host "Found include, skipping add."
        $hasAliasInclude = $true

if (!$hasAliasInclude) {
    Write-Host "Missing include, adding."
    $includeStatement = @"
`tpath =
    $includeStatement | Add-Content $env:userprofile\.gitconfig

I’m a principal engineer working at Microsoft. I have run and supported services for over twenty years and worked in multiple countries. I love working with passionate people and new technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.