Skip to content

Instantly share code, notes, and snippets.

@DineshSolanki
Created January 12, 2025 08:45
Show Gist options
  • Save DineshSolanki/861a3d7a39f13844498e6ec303ec1c94 to your computer and use it in GitHub Desktop.
Save DineshSolanki/861a3d7a39f13844498e6ec303ec1c94 to your computer and use it in GitHub Desktop.
Save Windows environment variables to restore to another PC
# Export current environment variables
$currentDate = Get-Date -Format "yyyyMMdd_HHmmss"
$exportPath = ".\env_backup_$currentDate"
# Create export directory if it doesn't exist
New-Item -ItemType Directory -Force -Path $exportPath
# Get all environment variables from different scopes
$machineEnv = [System.Environment]::GetEnvironmentVariables('Machine')
$userEnv = [System.Environment]::GetEnvironmentVariables('User')
$processEnv = [System.Environment]::GetEnvironmentVariables('Process')
# Export variables to individual files
$machineEnv | ConvertTo-Json | Set-Content "$exportPath\machine_env.json"
$userEnv | ConvertTo-Json | Set-Content "$exportPath\user_env.json"
$processEnv | ConvertTo-Json | Set-Content "$exportPath\process_env.json"
# Create restoration script
$restoreScript = @'
# Restore Environment Variables Script
param(
[Parameter(Mandatory=$true)]
[string]$BackupPath
)
function Set-EnvVar {
param(
[string]$Name,
[string]$Value,
[string]$Target
)
try {
[System.Environment]::SetEnvironmentVariable($Name, $Value, $Target)
Write-Host "Successfully set $Name in $Target scope" -ForegroundColor Green
}
catch {
Write-Warning "Failed to set $Name in $Target scope: $_"
}
}
# Verify backup path exists
if (-not (Test-Path $BackupPath)) {
Write-Error "Backup path not found: $BackupPath"
exit 1
}
# Restore Machine level variables (requires admin privileges)
$machineEnvPath = Join-Path $BackupPath "machine_env.json"
if (Test-Path $machineEnvPath) {
$machineEnv = Get-Content $machineEnvPath | ConvertFrom-Json
Write-Host "`nRestoring Machine environment variables..." -ForegroundColor Cyan
$machineEnv.PSObject.Properties | ForEach-Object {
Set-EnvVar -Name $_.Name -Value $_.Value -Target 'Machine'
}
}
# Restore User level variables
$userEnvPath = Join-Path $BackupPath "user_env.json"
if (Test-Path $userEnvPath) {
$userEnv = Get-Content $userEnvPath | ConvertFrom-Json
Write-Host "`nRestoring User environment variables..." -ForegroundColor Cyan
$userEnv.PSObject.Properties | ForEach-Object {
Set-EnvVar -Name $_.Name -Value $_.Value -Target 'User'
}
}
Write-Host "`nEnvironment variables restoration completed!" -ForegroundColor Green
Write-Host "Note: Some changes may require a system restart to take effect." -ForegroundColor Yellow
'@
# Save restoration script
$restoreScript | Set-Content "$exportPath\Restore-EnvVariables.ps1"
Write-Host "Environment variables have been exported to: $exportPath" -ForegroundColor Green
Write-Host "To restore these variables on another computer:" -ForegroundColor Cyan
Write-Host "1. Copy the entire '$exportPath' folder to the target computer"
Write-Host "2. Open PowerShell as Administrator"
Write-Host "3. Navigate to the backup folder"
Write-Host "4. Run: .\Restore-EnvVariables.ps1 -BackupPath ." -ForegroundColor Yellow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment