Background: Preterm infants have high rates of morbidity, especially from late-onset sepsis and necrotising enterocolitis. Lactoferrin is an anti-infective milk protein that may act through effects on gut bacteria, metabolites and epithelial cell function. The impact of supplemental lactoferrin in reducing late-onset sepsis was explored in the Enteral LactoFerrin In Neonates (ELFIN) trial. Objectives: The Mechanisms Affecting the Gut of Preterm Infants in Enteral feeding (MAGPIE) study was nested within the ELFIN trial and aimed to determine the impact of lactoferrin on gut microbiota and bacterial function, and changes preceding disease onset. We aimed to explore impacts on the stool bacteria and faecal/urinary metabolome using gas and liquid chromatography–mass spectrometry, and explore immunohistological pathways in resected tissue. Methods: Preterm infants from 12 NHS hospitals were enrolled in the study, and daily stool and urine samples were collected. Local sample collection data were combined with ELFIN trial data from the National Perinatal Epidemiology Unit, Oxford. The longitudinal impact of lactoferrin in healthy infants was determined, and samples that were collected before disease onset were matched with samples from healthy control infants. Established, quality-controlled 16S ribonucleic acid, gas chromatography–mass spectrometry and liquid chromatography–mass spectrometry analyses were conducted. Validated databases and standardised workflows were used to identify bacteria and metabolites. Tissue samples from infants undergoing surgery and matched controls were analysed. Results: We recruited 479 preterm infants (mean gestation of 28.4 ± 2.3 weeks) and collected > 33,000 usable samples from 467 infants. 16S ribonucleic acid bacterial analysis was conducted on samples from 201 infants, of whom 20 had necrotising enterocolitis and 51 had late-onset sepsis, along with samples from healthy matched controls to explore longitudinal changes. The greatest change in relative bacterial abundance over time was observed in Staphylococcus, which decreased from 42 at aged 7–9 days to only 2 at aged 30–60 days (p < 0.001). Small but significant differences in community composition were observed between samples in each ELFIN trial group (R2 = 0.005; p = 0.04). Staphylococcus (p < 0.01), Haemophilus (p < 0.01) and Lactobacillus (p = 0.01) showed greater mean relative abundance in the placebo group than in the lactoferrin group. Gas chromatography–mass spectrometry and liquid chromatography–mass spectrometry analyses showed that lactoferrin had limited impact on the metabolome. Liquid chromatography–mass spectrometry showed significant metabolite differences between necrotising enterocolitis or late-onset sepsis infants and healthy controls. The resected gut tissue analysis revealed 82 differentially expressed genes between healthy and necrotic tissue. Limitations: Although we recruited a large number of infants, collecting daily samples from every infant is challenging, especially in the few days immediately preceding disease onset. Conclusion: We conducted a large mechanistic study across multiple hospital sites and showed that, although lactoferrin significantly decreased the level of Staphylococcus and other key pathogens, the impact was smaller than those of other clinical variables. Immunohistochemistry identified multiple inflammatory pathways leading to necrotising enterocolitis and showed that the use of NHS pathology archive tissue is feasible in the context of a randomised controlled trial. Future work: We observed significant changes in the stool and urinary metabolome in cases preceding late-onset sepsis or necrotising enterocolitis, which provide metabolic targets for a future mechanistic and biomarker study. Trial registration: Current Controlled Trials ISRCTN12554594. Funding: This project was funded by the Efficacy and Mechanism Evaluation (EME) programme, a Medical Research Council (MRC) and National Institute for Health Research (NIHR) partnership. This will be published in full in Efficacy and Mechanism Evaluation; Vol. 8, No. 14. See the NIHR Journals Library website for further project information.