In this paper, a control approach is proposed for selective compensation of main voltage and current harmonics in grid-connected microgrids. Two modes of compensation are considered, i.e. voltage and current compensation modes. In the case that sensitive loads are connected to the point of common coupling (PCC), voltage compensation mode is activated in order to provide a high voltage quality at PCC. Otherwise, grid current harmonics are mitigated (current compensation mode) in order to avoid excessive harmonic supply by the grid. In both modes, harmonic compensation is achieved through proper control of distributed generators (DGs) interface converters. The compensation effort of each harmonic is shared considering the corresponding current harmonic supplied by the DGs. The control system of each DG comprises harmonic compensator, power controllers, voltage and current controllers and virtual impedance loop. Virtual impedance is considered at fundamental frequency to enhance power control and also at harmonic frequencies to improve the nonlinear load sharing among DGs. Simulation results are presented to demonstrate the effectiveness of the proposed method.