Promoted to captain, Ainsworth commanded Destroyer Squadron 2 in the Atlantic in 1940 and 1941 and then was assigned to Vice Admiral William F. Halsey's staff. At the end of 1941, Ainsworth took command of the battleship Mississippi. Promoted to rear admiral (July 1942), Ainsworth became commander, Destroyers, Pacific Fleet. He took a leading role in the Solomon Islands Campaign, commanding the bombardment of the Japanese airfield at Munda during 4–5 January 1943, long considered a textbook operation.
As commander of Cruiser Division 9 (January 1943–October 1944), Ainsworth commanded three cruisers and five destroyers escorting the U.S. invasion force to New Georgia. He fought in the Battle of Kula Gulf (5–6 July 1943), for which he was awarded the Navy Cross. He also fought in the Battle of Kolombangara (12–13 July 1943) and saw action in the Marianas, Guam, Leyte Gulf, and Peleliu. Ainsworth then commanded Cruisers and Destroyers, Pacific Fleet (October 1944–July 1945).
After the war, Ainsworth commanded the Fifth Naval District (August 1945–December 1948) until his retirement as a vice admiral. He died on 7 August 1960 in Bethesda, Maryland. The destroyer escort Ainsworth was named for him.
Morison, Samuel E. History of United States Naval Operations in World War II. Vol. 8, New Guinea and the Marianas, March 1944–August 1944. Boston: Little, Brown, 1953.; Morison, Samuel Eliot. History of United States Naval Operations in World War II. Vol. 5, The Struggle for Guadalcanal, August 1942–February 1943. Boston: Little, Brown, 1949.