This paper proposes a nonlinear model by an optimal stabilizing controller for weak/islanded grids using a unified power quality conditioner (UPQC). The UPQC can be employed to stabilize a grid-tie inverter (GTI) or a synchronous generator (SG) with minimum control effort. The idea that GTI behavior is like the synchronous generator is implemented in this study. The research aims at using an advanced controller to reduce oscillations and achieve stability in a micro-grid. Here, the robust sliding mode controller-based UPFC is employed to design an optimal grid stabilizer. The paper considers variations in UPQC terminal voltage during the transient period of the system, unlike other articles that assume it to be constant. The performance of the proposed algorithm is evaluated by two benchmark networks. The paper presents a comparative study of transient stability in a micro-grid system under different loads. Simulation results reflect the robustness of the proposed sliding mode controller for oscillation reduction in comparison with the Lyapunov-based nonlinear optimal controller and PI controller. In addition, results show the effectiveness of the proposed nonlinear controller in controlling both GTI and SG in the micro-grid system.