This paper deals with the optimal planning of the electrical energy storage systems in the microgrids aimed at cost minimization. The optimization accounts for the compensation of the voltage dips performed by the energy storage systems. A multi-step procedure, at the first step, identifies a set of candidate buses where the installation of a storage device produces the maximum benefit in terms of dip compensation; then, the life cycle costs in correspondence of different alternatives in terms of size and location of the storage systems are evaluated by considering an optimized use of the energy storage systems. The simulations on a medium voltage microgrid allowed validating the effectiveness of the proposed procedure.