8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

環境変数Pathのバックアップ

Posted at
  • PowerShellで、システム環境変数/ユーザー環境変数 両方のPathの値をファイル保存する
  • ;を改行コードで置換したファイルも保存しておくと比較しやすい
Backup.ps1
$now = Get-Date
$destDir = "backup/" + $now.ToString("yyyyMMdd_hhmmss")

# システム環境変数のPathを取得
$systemValue = [Environment]::GetEnvironmentVariable('PATH', 'Machine')
$systemFileName = Join-Path $destDir "system.txt"

# ユーザー環境変数のPathを取得
$userValue = [Environment]::GetEnvironmentVariable('PATH', 'User')
$userFileName = Join-Path $destDir "user.txt"

# システム環境変数のPathの";"を改行コードに置換
$systemValue2 = $systemValue.Replace(";", "`r`n")
$systemFileName2 = Join-Path $destDir "system2.txt"

# ユーザー環境変数のPathの";"を改行コードに置換
$userValue2 = $userValue.Replace(";", "`r`n")
$userFileName2 = Join-Path $destDir "user2.txt"

# フォルダーがなかったら作成
if (-not (Test-Path $destDir)){
    New-Item $destDir -Type Directory
}

# ファイル出力
Write-Output $systemValue | Out-File -FilePath $systemFileName -Encoding default
Write-Output $userValue | Out-File -FilePath $userFileName -Encoding default
Write-Output $systemValue2 | Out-File -FilePath $systemFileName2 -Encoding default
Write-Output $userValue2 | Out-File -FilePath $userFileName2 -Encoding default
8
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?