Create Windows 10 installer USB stick on macOS Catalina

Today my Windows 10 bricked itself after I enabled Hyper-V, I had no choice but to reinstall it. I only had my Macbook to create the USB stick. Here's how.

First of all, find out which device is your USB stick:

$ diskutil list
/dev/disk0 (internal, physical):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *251.0 GB disk0
1: EFI EFI 314.6 MB disk0s1
2: Apple_APFS Container disk1 185.0 GB disk0s2
3: Microsoft Basic Data BOOTCAMP 65.7 GB disk0s3

/dev/disk1 (synthesized):
#: TYPE NAME SIZE IDENTIFIER
0: APFS Container Scheme - +185.0 GB disk1
Physical Store disk0s2
1: APFS Volume Macintosh HD 11.1 GB disk1s1
2: APFS Volume Preboot 104.4 MB disk1s2
3: APFS Volume Recovery 1.0 GB disk1s3
4: APFS Volume VM 4.3 GB disk1s4
5: APFS Volume Macintosh HD - Data 158.7 GB disk1s5

/dev/disk2 (external, physical):
#: TYPE NAME SIZE IDENTIFIER
0: FDisk_partition_scheme *63.3 GB disk2
1: DOS_FAT_32 WIN10 63.3 GB disk2s1

/dev/disk3 (disk image):
#: TYPE NAME SIZE IDENTIFIER
0: CCCOMA_X64FRE_EN-US... +5.4 GB disk3

Here it is /dev/disk3. Erase the device with

diskutil eraseDisk MS-DOS WIN10 MBR /dev/disk3

Next go to https://www.microsoft.com/en-us/software-download/windows10ISO to get the installer ISO file. After that mount the ISO:

hdiutil mount ~/Downloads/Win10_1909_English_x64.iso

Now copy the files from the ISO to our USB stick, but not the oversized install.wim:

rsync -avh --progress --exclude=sources/install.wim /Volumes/CCCOMA_X64FRE_EN-US_DV9/ /Volumes/WIN10

Next up we'll need to install wimlib, a tool that will help us manipulate .wim files through Homebrew. If you haven't installed that already (how dare you!):

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Then we can install wimlib:

brew install wimlib

Now split the oversized install.wim and save the files to USB stick:

wimlib-imagex split /Volumes/CCCOMA_X64FRE_EN-US_DV9/sources/install.wim /Volumes/WIN10/sources/install.swm 4000

Finally we can eject the USB stick with:

hdiutil eject /dev/disk2

Now pull off your USB stick, plug it to your PC and boot it up to recover your machine! Good luck.