Skip to content

Instantly share code, notes, and snippets.

@rodnt
Created December 2, 2023 10:20
Show Gist options
  • Save rodnt/e9b60a59ebdb957206f2854b5f797fbe to your computer and use it in GitHub Desktop.
Save rodnt/e9b60a59ebdb957206f2854b5f797fbe to your computer and use it in GitHub Desktop.
Windows 11 bootable pendrive script
#!/bin/bash
# Function to select the USB device
select_usb_device() {
echo "Available USB devices:"
local devices=(/dev/sd*)
select usb_device in "${devices[@]}"; do
if [ -z "$usb_device" ]; then
echo "Invalid selection, please try again."
else
break
fi
done
echo "Selected USB device: $usb_device"
}
# Function to get the path to the Windows 11 ISO
get_iso_path() {
read -p "Enter the path to the Windows 11 ISO file: " iso_path
if [ ! -f "$iso_path" ]; then
echo "Invalid path or file does not exist. Exiting."
exit 1
fi
echo "Using ISO file: $iso_path"
}
# Select the USB device
select_usb_device
# Confirm with the user
read -p "Are you sure you want to proceed? (yes/no): " confirmation
if [ "$confirmation" != "yes" ]; then
echo "Exiting."
exit 1
fi
# Get the path to the Windows 11 ISO
get_iso_path
# Unmount partitions if already mounted
umount "${usb_device}1" 2>/dev/null
umount "${usb_device}2" 2>/dev/null
# Format the USB drive and create partitions
wipefs -a "$usb_device"
parted "$usb_device" <<EOF
mklabel gpt
mkpart BOOT fat32 0% 1GiB
mkpart INSTALL ntfs 1GiB 100%
quit
EOF
# Check the drive layout
parted "$usb_device" unit B print
# Mount Windows ISO
mkdir -p /mnt/iso
mount "$iso_path" /mnt/iso
# Format and mount the first partition
mkfs.vfat -n BOOT "${usb_device}1"
mkdir -p /mnt/vfat
mount "${usb_device}1" /mnt/vfat
# Copy files from ISO to the first partition
rsync -r --progress --exclude=sources --delete-before /mnt/iso/ /mnt/vfat/
# Copy boot.wim from ISO to sources directory
mkdir -p /mnt/vfat/sources
cp /mnt/iso/sources/boot.wim /mnt/vfat/sources/
# Format and mount the second partition
mkfs.ntfs --quick -L INSTALL "${usb_device}2"
mkdir -p /mnt/ntfs
mount "${usb_device}2" /mnt/ntfs
# Copy files from ISO to the second partition
rsync -r --progress --delete-before /mnt/iso/ /mnt/ntfs/
# Unmount all partitions and sync
umount /mnt/ntfs
umount /mnt/vfat
umount /mnt/iso
sync
# Power off the USB drive
udisksctl power-off -b "$usb_device"
echo "Windows 11 USB drive creation completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment