Create Windows 7/8/10 bootable USB drive in macOS with command line (MBR partitioning scheme)

In the following procedure we will create a Microsoft Windows bootable USB flash drive in macOS, from scratch with command line. Root privileges are required.
Note: This works only with BIOS or UEFI CSM (supports old BIOS style booting). Not compatible with native UEFI.

Test environment: macOS 10.13.1
Requirements: syslinux 4.03 and port of syslinux 4.03 to macOS

Warning: All the data on the USB flash drive will be destroyed without warning! Make sure you selected the correct drive or you will destroy a wrong disk!

1. Identify the USB flash drive, in my example the device is /dev/disk2.

diskutil list
/dev/disk0 (internal, physical):
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *121.3 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                 Apple_APFS Container disk1         121.1 GB   disk0s2

/dev/disk1 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +121.1 GB   disk1
                                 Physical Store disk0s2
   1:                APFS Volume SSD 128GB               34.8 GB    disk1s1
   2:                APFS Volume Preboot                 18.5 MB    disk1s2
   3:                APFS Volume Recovery                520.8 MB   disk1s3
   4:                APFS Volume VM                      1.1 GB     disk1s4

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

2. Erase the /dev/disk2, create one big partition /dev/disk2s1 and format as FAT32 with the label “WINDOWS”. All data on /dev/disk2 will be destroyed!

diskutil partitionDisk /dev/disk2 1 MBR FAT32 WINDOWS R
Started partitioning on disk2
Unmounting disk
Creating the partition map
Waiting for partitions to activate
Formatting disk2s1 as MS-DOS (FAT32) with name WINDOWS
512 bytes per physical sector
/dev/rdisk2s1: 30248048 sectors in 1890503 FAT32 clusters (8192 bytes/cluster)
bps=512 spc=16 res=32 nft=2 mid=0xf8 spt=32 hds=255 hid=2 drv=0x80 bsec=30277630 bspf=14770 rdcl=2 infs=1 bkbs=6
Mounting disk
Finished partitioning on disk2
/dev/disk2 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *15.5 GB    disk2
   1:                 DOS_FAT_32 WINDOWS                 15.5 GB    disk2s1

3. Set bootable flag on, start fdisk and enter the commands with red text.

fdisk -e /dev/disk2
Enter ‘help’ for information
fdisk: 1> flag 1
Partition 1 marked active.
fdisk:*1> write
Device could not be accessed exclusively.
A reboot will be needed for changes to take effect. OK? [n] y
Writing MBR at offset 0.
fdisk: 1> exit

4. Download and unpack syslinux 4.03 source code.

curl -o syslinux-4.03.tar.gz
tar -zxvf syslinux-4.03.tar.gz

5. Download syslinux macOS port and make it executable.

curl -o syslinux-mac.tar.gz
tar -zxvf syslinux-mac.tar.gz
chmod +x syslinux

6. Unmount and write the bootstrap code (mbr.bin) to the MBR of /dev/disk2.

diskutil unmountDisk /dev/disk2
Unmount of all volumes on disk2 was successful
dd if=syslinux-4.03/mbr/mbr.bin of=/dev/disk2 bs=440 count=1
1+0 records in
1+0 records out
440 bytes transferred in 0.000908 secs (484509 bytes/sec)

7. Install syslinux, this will alter the boot sector and copy ldlinux.sys into the root directory.

./syslinux -i /dev/disk2s1
syslinux for Mac OS X; created by Geza Kovacs for UNetbootin
"/dev/disk2s1" unmounted successfully.
/dev/disk2s1            DOS_FAT_32                      /Volumes/WINDOWS
mountpoint is /Volumes/WINDOWS
/Volumes/WINDOWS/ldlinux.sys ldlinuxname
"/dev/disk2s1" unmounted successfully.
/dev/disk2s1            DOS_FAT_32                      /Volumes/WINDOWS

8. Copy syslinux BIOS modules and create syslinux.cfg.

mkdir /Volumes/WINDOWS/syslinux
cp syslinux-4.03/com32/modules/*.c32 /Volumes/WINDOWS/syslinux

9. Create with a text editor /Volumes/WINDOWS/syslinux/syslinux.cfg and add the following:

default boot
LABEL boot
COM32 chain.c32
APPEND fs ntldr=/bootmgr

10. Mount the Microsoft Windows ISO image, please replace /example/windows.iso with the path of your Microsoft Windows image.

hdiutil mount /example/windows.iso -mountpoint /Volumes/WINSETUP

11. Copy the Windows installation files to the USB flash drive, it might take a few minutes.

cp -Rv /Volumes/WINSETUP/ /Volumes/WINDOWS/

12. Unmount the USB flash drive.

diskutil unmountDisk /dev/disk2