Cattle are the main reservoirs of Shiga toxin-producing Escherichia coli (STEC), a major foodborne pathogen associated with acute enteric disease and hemolytic–uremic syndrome in hu-mans. A total of 397 beef and dairy cattle from 5 farms were included in this study, of which 660 samples were collected for 16S rRNA gene sequencing. The microbiota of farms with a high-STEC prevalence (HSP) had greater richness compared to those of farms with a low-STEC prevalence (LSP). Longitudinal analyses showed STEC-shedders from LSP farms had higher microbiome di-versity; meanwhile, changes in the microbiome composition in HSP farms were independent of the STEC shedding status. Most of the bacterial genera associated with STEC shedding in dairy farms were also correlated with differences in the percentage of forage in diet and risk factors of STEC carriage such as days in milk, number of lactations, and warm temperatures. Identifying factors that alter the gut microbiota and enable STEC colonization in livestock could lead to novel strategies to prevent fecal shedding and the subsequent transmission to humans.