Distributed formation tracking control with obstacle avoidance of multiagent systems (MASs) under random switching topologies and external disturbances is considered in this article. To achieve the complex objective, an effective control strategy is developed in three steps. First, under the transition probability (TP)-based mode-dependent average dwell-time (MDADT) switching topologies, a distributed objective trajectory achieves almost sure global exponential tracking of the desired formation trajectory. Second, a safe objective trajectory approach is designed by geometrically projecting the unsafe parts of the existing formation trajectory onto the boundary of the obstacle region. Finally, an integral-multiplicative barrier Lyapunov function (IMBLF) is proposed to allow agents to track the safe objective trajectory, where the IMBLF can further guarantee the safety of the MASs. One of the interesting merits of our results is that the impulsive increasing of Lyapunov function at switching instants which is necessary for classical analysis methods has been removed. The feasibility of the proposed formation control method with obstacle avoidance is verified by simulations.