Neurotropic herpesviruses can establish lifelong infection in humans and contribute to severe diseases including encephalitis and neurodegeneration. However, the mechanisms through which the brain’s immune system recognizes and controls viral infections propagating across synaptically linked neuronal circuits have remained unclear. Using a well-established model of alphaherpesvirus infection that reaches the brain exclusively via retrograde transsynaptic spread from the periphery, and in vivo two-photon imaging combined with high resolution microscopy, we show that microglia are recruited to and isolate infected neurons within hours. Selective elimination of microglia results in a marked increase in the spread of infection and egress of viral particles into the brain parenchyma, which are associated with diverse neurological symptoms. Microglia recruitment and clearance of infected cells require cell-autonomous P2Y12 signalling in microglia, triggered by nucleotides released from affected neurons. In turn, we identify microglia as key contributors to monocyte recruitment into the inflamed brain, which process is largely independent of P2Y12. P2Y12-positive microglia are also recruited to infected neurons in the human brain during viral encephalitis and both microglial responses and leukocyte numbers correlate with the severity of infection. Thus, our data identify a key role for microglial P2Y12 in defence against neurotropic viruses, whilst P2Y12-independent actions of microglia may contribute to neuroinflammation by facilitating monocyte recruitment to the sites of infection.Electronic supplementary materialThe online version of this article (10.1007/s00401-018-1885-0) contains supplementary material, which is available to authorized users.