Consuming a high-fat diet (HFD) is widely recognized to cause obesity and result in chronic brain inflammation that impairs cognitive function. Repetitive transcranial magnetic stimulation (rTMS) has shown effectiveness in both weight loss and cognitive improvement, although the exact mechanism is still unknown. Our study examined the effects of rTMS on the brain and intestinal microecological dysfunction. rTMS successfully reduced cognitive decline caused by an HFD in behavioral assessments involving the Y maze and novel object recognition. This was accompanied by an increase in the number of new neurons and the transcription level of genes related to synaptic plasticity (spindlin 1, synaptophysin, and postsynaptic protein-95) in the hippocampus. It was reached that rTMS decreased the release of high mobility group box 1, activation of microglia, and inflammation in the brains of HFD rats. rTMS also reduced hypothalamic hypocretin levels and improved peripheral blood lipid metabolism. In addition, rTMS recovered the HFD-induced gut microbiome imbalances, metabolic disorders, and, in particular, reduced levels of the microvirus. Our research emphasized that rTMS enhanced cognitive abilities, resulting in positive impacts on brain inflammation, neurodegeneration, and the microbiota in the gut, indicating the potential connection between the brain and gut, proposing that rTMS could be a new approach to addressing cognitive deficits linked to obesity.