This study aimed to investigate the microbial structure and function in the rectum of weaned piglets with berberine supplementation. Twelve healthy 21-day-old Duorc × (Landrace × Large White) weaned piglets (similar body weight) were evenly divided into control and berberine groups and were fed a basal diet supplemented with 0 and 0.1% berberine, respectively. After 21 days, metagenomic sequencing analysis was performed to detect microbial composition and function in the rectum of weaned piglets. Results showed that there were 10,597,721,931–14,059,392,900 base pairs (bp) and 10,186,558,171–15,859,563,342 bp of clean data in the control and berberine groups, respectively. The Q20s of the control and berberine groups were 97.15 to 97.7% and 96.26 to 97.68%, respectively. The microorganisms in the berberine group had lower (p < 0.05) Chao1, alternating conditional expectation, Shannon, and Simpson indices at the species levels than those in the control group. Analysis of similarity showed that there were significant differences (p < 0.01) between the control and berberine groups at the genus and species levels of the gut microorganisms. Dietary berberine significantly increased (p < 0.05) the abundance of Subdoligranulum variabile, but decreased (p < 0.05) the abundance of Prevotella copri compared with the control group. Carbohydrate-active enzymes analysis revealed that the levels of polysaccharide lyases and carbohydrate esterases were lower (p < 0.05) in the berberine group than that in the control group. Linear discriminant analysis effect size analysis showed that berberine supplementation could induce various significant Kyoto Encyclopedia of Genes and Genomes pathways, including carbohydrate metabolism, environmental information processing, and microbial metabolism in diverse environments. In conclusion, our findings suggest that berberine could improve the composition, abundance, structure, and function of gut microbiome in the weaned piglets, potentially providing a suitable approach for the application of berberine in human and animal health.