Herpes zoster (HZ) is caused by reactivation of the neurotrophic varicella-zoster virus (VZV). Zoster may contribute to development of dementia through neuroinflammation, cerebral vasculopathy, or direct neural damage, but epidemiologic evidence is limited. We used data from linked nationwide Danish registries to conduct a cohort study of the association between zoster and dementia during 1997-2017. As secondary aims, we examined whether associations were more pronounced for zoster involving cranial nerves (mainly ophthalmic zoster) or the CNS and Alzheimer disease as an outcome. We included people aged ≥40 years with zoster and a general population comparison cohort matched 5:1 by sex and birth year. We identified zoster and dementia in the registries using prescription records in the community and hospital diagnoses. We used Cox regression to compute confounder-adjusted hazard ratios (HRs) with 95% CIs for dementia associated with zoster during 0-1 year and 1-21 years of follow-up. We compared the cumulative incidence of dementia, inverse probability weighted for confounders. The study included 247,305 people with zoster and 1,235,890 matched general population comparators (median age 64 years; 61% female). The HR of all-cause dementia was 0.98 (95% CI 0.92-1.04) during the first year and 0.93 (95% CI 0.90-0.95) thereafter in people with zoster vs matched comparators. Dementia was diagnosed in 9.7% of patients with zoster and 10.3% of matched comparators by the end of follow-up. We observed no increased long-term risk of dementia in subgroup analyses, except possibly in people with CNS infection (HR 1.94; 95% CI 0.78-4.80). Analyses of Alzheimer disease as a separate outcome showed similar results. HZ is not associated with an increased risk of dementia, and contrary to expectation, we found a small decrease in the risk. The explanation for this finding is unclear, and systematic errors should be considered. Patients with CNS involvement had an almost 2-fold increased relative risk of dementia. The population attributable fraction of dementia due to this rare complication is estimated at 0.014%. Therefore, universal vaccination against VZV in the elderly is unlikely to reduce dementia risk.