Psychiatric outcomes after COVID-19 have been of high concern during the pandemic; however, studies on a nationwide level are lacking. To estimate the risk of mental disorders and use of psychotropic medication among individuals with COVID-19 compared with individuals not tested, individuals with SARS-CoV-2-negative test results, and those hospitalized for non-COVID-19 infections. This nationwide cohort study used Danish registries to identify all individuals who were alive, 18 years or older, and residing in Denmark between January 1 and March 1, 2020 (N = 4 152 792), excluding individuals with a mental disorder history (n = 616 546), with follow-up until December 31, 2021. Results of SARS-CoV-2 polymerase chain reaction (PCR) testing (negative, positive, and never tested) and COVID-19 hospitalization. Risk of new-onset mental disorders (International Statistical Classification of Diseases and Related Health Problems, Tenth Revision, codes F00-F99) and redeemed psychotropic medication (Anatomical Therapeutic Chemical classification codes N05-N06) was estimated through survival analysis using a Cox proportional hazards model, with a hierarchical time-varying exposure, reporting hazard rate ratios (HRR) with 95% CIs. All outcomes were adjusted for age, sex, parental history of mental illness, Charlson Comorbidity Index, educational level, income, and job status. A total of 526 749 individuals had positive test results for SARS-CoV-2 (50.2% men; mean [SD] age, 41.18 [17.06] years), while 3 124 933 had negative test results (50.6% women; mean [SD] age, 49.36 [19.00] years), and 501 110 had no tests performed (54.6% men; mean [SD] age, 60.71 [19.78] years). Follow-up time was 1.83 years for 93.4% of the population. The risk of mental disorders was increased in individuals with positive (HRR, 1.24 [95% CI, 1.17-1.31]) and negative (HRR, 1.42 [95% CI, 1.38-1.46]) test results for SARS-CoV-2 compared with those never tested. Compared with individuals with negative test results, the risk of new-onset mental disorders in SARS-CoV-2-positive individuals was lower in the group aged 18 to 29 years (HRR, 0.75 [95% CI, 0.69-0.81]), whereas individuals 70 years or older had an increased risk (HRR, 1.25 [95% CI, 1.05-1.50]). A similar pattern was seen regarding psychotropic medication use, with a decreased risk in the group aged 18 to 29 years (HRR, 0.81 [95% CI, 0.76-0.85]) and elevated risk in those 70 years or older (HRR, 1.57 [95% CI, 1.45-1.70]). The risk for new-onset mental disorders was substantially elevated in hospitalized patients with COVID-19 compared with the general population (HRR, 2.54 [95% CI, 2.06-3.14]); however, no significant difference in risk was seen when compared with hospitalization for non-COVID-19 respiratory tract infections (HRR, 1.03 [95% CI, 0.82-1.29]). In this Danish nationwide cohort study, overall risk of new-onset mental disorders in SARS-CoV-2-positive individuals did not exceed the risk among individuals with negative test results (except for those aged ≥70 years). However, when hospitalized, patients with COVID-19 had markedly increased risk compared with the general population, but comparable to risk among patients hospitalized for non-COVID-19 infections. Future studies should include even longer follow-up time and preferentially include immunological biomarkers to further investigate the impact of infection severity on postinfectious mental disorder sequelae.