Microgrids are composed of distributed energy resources (DERs), storage devices, electric vehicles, flexible loads and so on. They may either operate connected to the main electricity grid (on-grid operation) or separated from the grid (islanded operation). The outputs of the renewable energy sources may fluctuate and thus can cause deviations in the voltage magnitudes especially at islanded mode. This may affect the stability of the microgrids. This paper proposes an optimization model to efficiently manage controllable devices in microgrids aiming to minimize the voltage deviations both in on-grid and islanded operation modes. RSE Distributed Energy Resources Test Facility (DER-TF), which is a low voltage microgrid system in Italy, is used to verify the algorithm. The test system's data is taken through an online software system (REDIS) and a harmony search based optimization algorithm is applied to control the device parameters. The experimental results show that the harmony search based optimization approach successfully finds the control parameters, and can help the system to obtain a better voltage profile.