This paper describes measurements of the transverse momentum spectra of W and Z bosons produced in proton–proton collisions at centre-of-mass energies of s=5.02 TeV and s=13 TeV with the ATLAS experiment at the Large Hadron Collider. Measurements are performed in the electron and muon channels, W→ℓν and Z→ℓℓ (ℓ=e or μ), and for W events further separated by charge. The data were collected in 2017 and 2018, in dedicated runs with reduced instantaneous luminosity, and correspond to 255 and 338 pb-1 at s=5.02 TeV and 13 TeV, respectively. These conditions optimise the reconstruction of the W-boson transverse momentum. The distributions observed in the electron and muon channels are unfolded, combined, and compared to QCD calculations based on parton shower Monte Carlo event generators and analytical resummation. The description of the transverse momentum distributions by Monte Carlo event generators is imperfect and shows significant differences largely common to W-, W+ and Z production. The agreement is better at s=5.02 TeV, especially for predictions that were tuned to Z production data at s=7 TeV. Higher-order, resummed predictions based on DYTurbo generally match the data best across the spectra. Distribution ratios are also presented and test the understanding of differences between the production processes.