Understanding the impact of SARS-CoV-2 infection and COVID-19 vaccination in pregnancy on neonatal and maternal outcomes informs clinical decision-making. Here we report a national, population-based, matched cohort study to investigate associations between SARS-CoV-2 infection and, separately, COVID-19 vaccination just before or during pregnancy and the risk of adverse neonatal and maternal outcomes among women in Scotland with a singleton pregnancy ending at ≥20 weeks gestation. Neonatal outcomes are stillbirth, neonatal death, extended perinatal mortality, preterm birth (overall, spontaneous, and provider-initiated), small-for-gestational age, and low Apgar score. Maternal outcomes are admission to critical care or death, venous thromboembolism, hypertensive disorders of pregnancy, and pregnancy-related bleeding. We use conditional logistic regression to derive odds ratios adjusted for socio-demographic and clinical characteristics (aORs). We find that infection is associated with an increased risk of preterm (aOR=1.36, 95% Confidence Interval [CI]=1.16-1.59) and very preterm birth (aOR=1.90, 95% CI 1.20-3.02), maternal admission to critical care or death (aOR=1.72, 95% CI=1.39-2.12), and venous thromboembolism (aOR=2.53, 95% CI=1.47-4.35). We find no evidence of increased risk for any of our outcomes following vaccination. These data suggest SARS-CoV-2 infection during pregnancy is associated with adverse neonatal and maternal outcomes, and COVID-19 vaccination remains a safe way for pregnant women to protect themselves and their babies against infection.