We introduce a variational total-energy functional to treat finite homogeneous electric fields with periodic boundary conditions and show that this functional can be implemented within a Car-Parrinello molecular dynamics scheme. The Coupling to an electric field is achieved through the Berry-phase expression of the polarization. The minimization of this extended functional gives a ground state which describes the polarized state in ail electric field. For a crystalline system, the ground state of this extended functional preserves the Bloch symmetry. The reliability of the method is demonstrated in the case of bulk MgO for the Born effective charges, and the high- and low-frequency dielectric constants. In the latter case, we evaluated the static dielectric constant by performing a damped molecular dynamics in the presence of a finite electric field, completely avoiding the calculation of the dynamical matrix.