Clinical and animal studies have shown that gut microbiome disturbances can affect neural function and behaviors via the microbiota–gut–brain axis, and may be implicated in the pathogenesis of several brain diseases. However, exactly how the gut microbiome modulates nervous system activity remains obscure. Here, using a single-cell nucleus sequencing approach, we sought to characterize the cell type–specific transcriptomic changes in the prefrontal cortex and hippocampus derived from germ-free (GF), specific pathogen free, and colonized-GF mice. We found that the absence of gut microbiota resulted in cell-specific transcriptomic changes. Furthermore, microglia transcriptomes were preferentially influenced, which could be effectively reversed by microbial colonization. Significantly, the gut microbiome modulated the mutual transformation of microglial subpopulations in the two regions. Cross-species analysis showed that the transcriptome changes of these microglial subpopulations were mainly associated with Alzheimer’s disease (AD) and major depressive disorder (MDD), which were further supported by animal behavioral tests. Our findings demonstrate that gut microbiota mainly modulate the mutual transformation of microglial subtypes, which may lead to new insights into the pathogenesis of AD and MDD.