この関数は、変更したいディストリビューションの現在の名前と新しい名前を引数として受け取ります。
function Change-WSLDistributionName {
param (
[string]$CurrentName,
[string]$NewName
)
# 現在のディストリビューション名に基づいてレジストリキーを検索
$distributions = Get-ChildItem -Path 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\'
$targetDistribution = $distributions | Where-Object { (Get-ItemProperty -Path $_.PsPath).DistributionName -eq $CurrentName }
if ($targetDistribution -ne $null) {
# DistributionNameを新しい名前に変更
Set-ItemProperty -Path $targetDistribution.PsPath -Name 'DistributionName' -Value $NewName
Write-Output "ディストリビューション名が`'$CurrentName'`から`'$NewName'`に変更されました。"
}
else {
Write-Output "指定されたディストリビューション名`'$CurrentName'`が見つかりません。"
}
}
この関数を使ってディストリビューション名を変更するには、次のようにコマンドを実行します。
Change-WSLDistributionName -CurrentName '旧ディストリビューション名' -NewName '新ディストリビューション名'
例えば、現在のディストリビューション名がUbuntu
で、これをMyUbuntu
に変更したい場合は、以下のようになります。
Change-WSLDistributionName -CurrentName 'Ubuntu' -NewName 'MyUbuntu'
この関数を実行すると、指定したディストリビューションの名前が変更されます。
変更が成功したかどうかを確認するには、再びwsl -l -v
を実行してください。