Skip to content

Instantly share code, notes, and snippets.

@ShortArrow
Created February 24, 2024 01:35
Show Gist options
  • Save ShortArrow/c1360d93410f7e1f720df86dace3a4ac to your computer and use it in GitHub Desktop.
Save ShortArrow/c1360d93410f7e1f720df86dace3a4ac to your computer and use it in GitHub Desktop.
WSLのDistroの名前を書き換える

この関数は、変更したいディストリビューションの現在の名前と新しい名前を引数として受け取ります。

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を実行してください。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment