  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 $\sqrt{s}=5.02$ TeV and $\sqrt{s}=13$ TeV with the ATLAS experiment at the
Large Hadron Collider. Measurements are performed in the electron and muon
channels, $W \to \ell\nu$ and $Z \to \ell\ell$ ($\ell=e$ or $\mu$), 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$ pb$^{-1}$ and $338$ pb$^{-1}$ at $\sqrt{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 $\sqrt{s}=5.02$
TeV, especially for predictions that were tuned to $Z$ production data at
$\sqrt{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.
