Attitude stabilization of spacecraft using magnetorquers can be achieved by a proportional–derivative-like control algorithm. The gains of this algorithm are usually determined by using a trial-and-error approach within the large search space of the possible values of the gains. However, when finding the gains in this manner, only a small portion of the search space is actually explored. We propose here an innovative and systematic approach for finding the gains: they should be those that minimize the settling time of the attitude error. However, the settling time depends also on initial conditions. Consequently, gains that minimize the settling time for specific initial conditions cannot guarantee the minimum settling time under different initial conditions. Initial conditions are not known in advance. We overcome this obstacle by formulating a min–max problem whose solution provides robust gains, which are gains that minimize the settling time under the worst initial conditions, thus producing good average behavior. An additional difficulty is that the settling time cannot be expressed in analytical form as a function of gains and initial conditions. Hence, our approach uses some derivative-free optimization algorithms as building blocks. These algorithms work without the need to write the objective function analytically: they only need to compute it at a number of points. Results obtained in a case study are very promising.