Many genetic and environmental factors increase susceptibility to cognitive impairment (CI), and the gut microbiome is increasingly implicated. However, the identity of gut microbes associated with CI risk, their effects on CI, and their mechanisms remain unclear. Here, we show that a carbohydrate-restricted (ketogenic) diet potentiates CI induced by intermittent hypoxia in mice and alters the gut microbiota. Depleting the microbiome reduces CI, whereas transplantation of the risk-associated microbiome or monocolonization with Bilophila wadsworthia confers CI in mice fed a standard diet. B. wadsworthia and the risk-associated microbiome disrupt hippocampal synaptic plasticity, neurogenesis, and gene expression. The CI is associated with microbiome-dependent increases in intestinal interferon-gamma (IFNg)-producing Th1 cells. Inhibiting Th1 cell development abrogates the adverse effects of both B. wadsworthia and environmental risk factors on CI. Together, these findings identify select gut bacteria that contribute to environmental risk for CI in mice by promoting inflammation and hippocampal dysfunction.