In a thermal field theory, the cumulants of the momentum distribution can be extracted from the dependence of the Euclidean path integral on a shift in the fields built into the temporal boundary condition. When combined with the Ward identities associated with the invariance of the theory under the Poincare' group, thermodynamic potentials such as the entropy or the pressure can be directly inferred from the response of the system to the shift. Crucially the argument holds, up to harmless finite-size and discretization effects, even if translational and rotational invariance are broken to a discrete subgroup of finite shifts and rotations such as in a lattice box. The formulas are thus applicable at finite lattice spacing and volume provided the derivatives are replaced by their discrete counterpart, and no additive or multiplicative ultraviolet-divergent renormalizations are needed to take the continuum limit. In this paper we present a complete derivation of the relevant formulas in the scalar field theory, where several technical complications are avoided with respect to gauge theories. As a by-product we obtain a recursion relation among the cumulants of the momentum distribution, and formulae for finite-volume corrections to several well-known thermodynamic identities.