A new method for optimally dimensioning an energy storage system (ESS) in a microgrid (MG) to store electrical / renewable energy at the time of surplus and to re-dispatch. The problem of unit commitment with the spinning reserve for MG is considered in this new method. The total cost function is entered, which includes the cost of the ESS, the cost of the output power and the cost of the spinning reserve. The efficacy of the approach is validated through a case study in which the optimal ESS classification for MG is determined. The quantitative results show that the ESS with an optimal size is not only to store renewable energy and re-dispatch it properly, but also to save the total cost for MG. The main method is formulated as a mixed nonlinear integer problem (MNIP), which is solved in AMPL (A Modeling Language for Mathematical Programming). The problem of unit commitment, which is crucial for the proper functioning of the microgrid, has also been considered in the present work.