The direct and optimal control solution of laminar fully developed, steady Magnetohydrodynamics (MHD) flow of an incompressible, electrically conducting power-law non-Newtonian fluid in a square duct is considered with the heat transfer. The fluid is subjected to an external uniform magnetic field as well as a constant pressure gradient. The apparent fluid viscosity is both a function of the unknown velocity and the flow index which makes the momentum equation nonlinear. Viscous and Joule dissipation terms are also included. The direct problem is solved by using Galerkin finite element method (FEM) with mixed finite elements and the control problem approach is the discretize-then-optimize procedure. The control formulations with the flow index parameter and the Hartmann number are given to regain the desired velocity profile and temperature isolines of the MHD flow.