A new method is presented for performing first-principles molecular-dynamics simulations of systems with variable occupancies. We adopt a matrix representation for the one-particle statistical operator Γ, to introduce a "projected" free energy functional G that depends on the Kohn-Sham orbitals only and that is invariant under their unitary transformations. The Liouville equation [Γ,Ĥ] = 0 is always satisfied, guaranteeing a very efficient and stable variational minimization algorithm that can be extended to non-conventional entropic formulations or fictitious thermal distributions.In recent years, the range of problems that can be studied with quantitative accuracy using the methods of computational solid state physics has expanded dramatically. It is now possible to calculate many materials properties with an accuracy that is often comparable to that of experiments. This degree of confidence is based on the fundamental quantum-mechanical treatment offered by density-functional theory (DFT) [1], coupled with the availability of increasingly powerful computers and with the development of algorithms tuned towards optimal performance [2].The application of these methods and techniques to metallic systems has nonetheless encountered several difficulties, that have made progress slower than for the case of semiconductors and insulators. The discontinuous variation of the orbital occupancies across the Brillouin zone (BZ) makes the occupation numbers rather ill-conditioned variables, and the self-consistent solution of the screening problem can suffer from several instabilities. The absence of a gap in the energy spectrum and the requirement of an exact diagonalization for the Hamiltonian matrix everywhere in the BZ (in order to assign the occupation numbers) introduce "slow frequencies" in the evolution of the orbitals towards the ground state and preclude the straightforward extension to metals of algorithms which performed well for insulators. Smearing the Fermi surface with a finite electronic temperature [3] allows for an improved BZ sampling, but only partially alleviates the problems alluded to above.In this Letter, we introduce a new approach which solves many of these problems in a natural way, and which provides a general and efficient framework for obtaining the ground state of a Kohn-Sham Hamiltonian at a finite electronic temperature. The typical context is the Mermin formulation for the Fermi-Dirac statistics [4], but the method also applies when generalized entropic functionals are introduced [5], as it is often the case for metallic systems. Other applications include DFT studies of insulators or semiconductors with thermally excited states [6], and fractional quantum Hall states [7]. The language of ensemble-DFT [8] is used, and an orbitalbased variational algorithm for the minimization to the ground state is developed and implemented. Dramatic improvements are obtained in the convergence of the energies and especially of the Hellmann-Feynman forces.Within ensemble-DFT, the Helmholtz free energy fun...