Evidence on associations between COVID-19 vaccination or SARS-CoV-2 infection and the risk of congenital anomalies is limited. Here we report a national, population-based, matched cohort study using linked electronic health records from Scotland (May 2020-April 2022) to estimate the association between COVID-19 vaccination and, separately, SARS-CoV-2 infection between six weeks pre-conception and 19 weeks and six days gestation and the risk of  any major congenital anomaly and  any non-genetic major congenital anomaly. Mothers vaccinated in this pregnancy exposure period mostly received an mRNA vaccine (73.7% Pfizer-BioNTech BNT162b2 and 7.9% Moderna mRNA-1273). Of the 6731 babies whose mothers were vaccinated in the pregnancy exposure period, 153 had any anomaly and 120 had a non-genetic anomaly. Primary analyses find no association between any vaccination and any anomaly (adjusted Odds Ratio [aOR] = 1.01, 95% Confidence Interval [CI] = 0.83-1.24) or non-genetic anomalies (aOR = 1.00, 95% CI = 0.81-1.22). Primary analyses also find no association between SARS-CoV-2 infection and any anomaly (aOR = 1.02, 95% CI = 0.66-1.60) or non-genetic anomalies (aOR = 0.94, 95% CI = 0.57-1.54). Findings are robust to sensitivity analyses. These data provide reassurance on the safety of vaccination, in particular mRNA vaccines, just before or in early pregnancy.