Antibiotics are frequently used to treat dairy cows with mastitis. However, the potential effects of β-lactam antibiotics, such as cephalosporins, on the fecal microbiome is unknown. The objective was to investigate the effects of ceftiofur and cefquinome on the fecal microbiota and antibiotic resistance genes of dairy cows with mastitis. The fecal samples were collected from 8 dairy cows at the following periods: the start day (Day 0), medication (Days 1, 2, and 3), withdrawal (Days 4, 6, 7, and 8), and recovery (Days 9, 11, 13, and 15). 16S rRNA gene sequencing was applied to explore the changes in microbiota, and qPCR was used to investigate the antibiotic resistance genes. The cephalosporin treatment significantly decreased the microbial diversity and richness, indicated by the decreased Shannon and Chao 1 indexes, respectively (p < 0.05). The relative abundance of Bacteroides, Bacteroidaceae, Bacteroidales, and Bacteroidia increased, and the relative abundance of Clostridia, Clostridiales, Ethanoligenens, and Clostridium IV decreased at the withdrawal period. The cephalosporin treatment increased the relative abundance of β-lactam resistance genes (blaTEM and cfxA) at the withdrawal period (p < 0.05). In conclusion, the cephalosporin treatment decreased the microbial diversity and richness at the medication period, and increased the relative abundance of two β-lactam resistance genes at the withdrawal period.