Skip to content

Git for Windows

David Segura - PowerShell MVP edited this page May 16, 2025 · 9 revisions

Git for Windows

Write-Host "Checking if Git is already installed..." -ForegroundColor Cyan
$git = Get-Command git -ErrorAction SilentlyContinue
if ($git) {
    $gitVersion = git --version
    Write-Host "Git is already installed: $gitVersion" -ForegroundColor Green
    exit 0
}

Write-Host "Git is not installed. Installing Git for Windows using winget..." -ForegroundColor Yellow
try {
    winget install -e --id Git.Git -h
    if ($LASTEXITCODE -eq 0) {
        Write-Host "Git for Windows installed successfully." -ForegroundColor Green
    } else {
        Write-Host "Git installation may have failed. Please check the output above." -ForegroundColor Red
        exit 1
    }
} catch {
    Write-Host "An error occurred during installation: $_" -ForegroundColor Red
    exit 1
}
Write-Host "Checking if Git is installed..." -ForegroundColor Cyan
$git = Get-Command git -ErrorAction SilentlyContinue
if (-not $git) {
    Write-Host "Git is not installed. Please install Git before running this script." -ForegroundColor Red
    exit 1
}

# Check if user.email is set
$currentEmail = git config --global user.email
if ([string]::IsNullOrWhiteSpace($currentEmail)) {
    $userEmail = Read-Host "Enter your Git email address"
    git config --global user.email "$userEmail"
    Write-Host "Set git user.email to $userEmail" -ForegroundColor Green
} else {
    Write-Host "Current git user.email: $currentEmail" -ForegroundColor Yellow
    $changeEmail = Read-Host "Do you want to change it? (y/n)"
    if ($changeEmail -eq 'y') {
        $userEmail = Read-Host "Enter your new Git email address"
        git config --global user.email "$userEmail"
        Write-Host "Updated git user.email to $userEmail" -ForegroundColor Green
    }
}

# Check if user.name is set
$currentName = git config --global user.name
if ([string]::IsNullOrWhiteSpace($currentName)) {
    $userName = Read-Host "Enter your Git user name"
    git config --global user.name "$userName"
    Write-Host "Set git user.name to $userName" -ForegroundColor Green
} else {
    Write-Host "Current git user.name: $currentName" -ForegroundColor Yellow
    $changeName = Read-Host "Do you want to change it? (y/n)"
    if ($changeName -eq 'y') {
        $userName = Read-Host "Enter your new Git user name"
        git config --global user.name "$userName"
        Write-Host "Updated git user.name to $userName" -ForegroundColor Green
    }
}

# Show final identity
$finalEmail = git config --global user.email
$finalName = git config --global user.name
Write-Host "\nYour global Git identity is now:" -ForegroundColor Cyan
Write-Host "  Name : $finalName" -ForegroundColor White
Write-Host "  Email: $finalEmail" -ForegroundColor White

Clone this wiki locally