# Simple Bitwarden .env Sync for Windows # Run with: powershell -ExecutionPolicy Bypass -File sync-env.ps1 Write-Host "=== Bitwarden .env Sync ===" -ForegroundColor Cyan # Check if bw is installed if (!(Get-Command bw -ErrorAction SilentlyContinue)) { Write-Host "Error: Bitwarden CLI (bw) is not installed" -ForegroundColor Red Write-Host "Install it from: https://bitwarden.com/help/cli/" exit 1 } # Check status $statusOutput = bw status 2>$null if ($LASTEXITCODE -ne 0) { Write-Host "Error: Failed to get Bitwarden status" -ForegroundColor Red exit 1 } $status = $statusOutput | ConvertFrom-Json if ($status.status -eq "unauthenticated") { Write-Host "Please login to Bitwarden:" bw login if ($LASTEXITCODE -ne 0) { Write-Host "Error: Failed to login to Bitwarden" -ForegroundColor Red exit 1 } $statusOutput = bw status 2>$null $status = $statusOutput | ConvertFrom-Json } # Unlock if needed if ($status.status -eq "locked") { Write-Host "Unlocking vault..." Write-Host "Please enter your Bitwarden master password:" $sessionKey = bw unlock --raw if ($LASTEXITCODE -ne 0) { Write-Host "Error: Failed to unlock vault" -ForegroundColor Red exit 1 } $env:BW_SESSION = $sessionKey } # Sync Write-Host "Syncing with Bitwarden..." bw sync 2>$null | Out-Null if ($LASTEXITCODE -ne 0) { Write-Host "Error: Failed to sync with Bitwarden" -ForegroundColor Red exit 1 } # CHANGE THIS to your Bitwarden item name $itemName = "portfolio-env" Write-Host "Fetching environment variables..." # Get item $itemOutput = bw get item $itemName 2>$null if ($LASTEXITCODE -ne 0) { Write-Host "Error: Could not find item '$itemName' in Bitwarden" -ForegroundColor Red Write-Host "Make sure you have an item with this exact name in your vault" exit 1 } $item = $itemOutput | ConvertFrom-Json # Get notes and process them if (!$item.notes) { Write-Host "Error: No notes found in item '$itemName'" -ForegroundColor Red Write-Host "Add your environment variables to the notes field" exit 1 } # Process notes - handle escaped newlines and other JSON formatting $notes = $item.notes -replace '\\n', "`n" -replace '\\r', "`r" -replace '\\t', "`t" # Create .env file $envPath = Join-Path (Get-Location) ".env" $notes | Out-File -FilePath $envPath -Encoding UTF8 -NoNewline Write-Host "Created .env file successfully at: $envPath" -ForegroundColor Green