SPDX-License-Identifier: GPL-3.0-or-later
A PowerShell script to compact the VHDX file of a Windows Subsystem for Linux (WSL) instance. This script uses diskpart to compact the VHDX file, allowing for reduced disk space usage and improved performance.
Copyright (C) 2024 Andrew Revak
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
This PowerShell script compacts the VHDX file of a specified Windows Subsystem for Linux (WSL) instance, helping to reclaim disk space.
.\Compact-WSL-InstanceVHD.ps1 [-InstanceName <string>] [-DryRun] [-Help]
-InstanceName <string>
: Specify the WSL instance name. If not provided, a selection menu will be displayed.-DryRun
: Simulate the actions without making any changes.-Help
: Display the help message.
.\Compact-WSL-InstanceVHD.ps1 -InstanceName "Ubuntu-22.04" -DryRun
.\Compact-WSL-InstanceVHD.ps1
The script performs the following actions:
- Verifies that it's running with administrator privileges.
- If no instance name is provided, it displays a list of available WSL instances for selection.
- Locates the VHDX file for the specified WSL instance using the Windows Registry.
- Terminates the WSL instance.
- Creates a diskpart script to compact the VHDX file.
- Runs the diskpart script to perform the compaction.
- Restarts the WSL instance.
- This script requires administrator privileges to run.
- It's recommended to back up your WSL instance before running this script.
- The script includes a dry run option to preview the actions without making changes.
- Administrator privileges