WASHINGTON — President Trump on Thursday abruptly decided to install Joseph Maguire, the director of the National Counterterrorism Center, as the acting director of national intelligence after Dan Coats steps down from the post next week.

Mr. Trump announced his decision to elevate Mr. Maguire, a retired vice admiral who once led the Navy’s Special Warfare Command, on Twitter shortly after confirming that Sue Gordon, the nation’s No. 2 intelligence official — who by law had been in line to temporarily take over as director — would instead depart with Mr. Coats on Aug. 15.

Ms. Gordon, who served more than 30 years in intelligence posts at the C.I.A. and other agencies, informed Mr. Trump of her decision to retire in a letter on Thursday after it became clear that he would not permit her to rise to the position of acting director.

“As you ask a new leadership team to take the helm, I will resign my position,” she wrote, adding: “I am confident in what the intelligence community has accomplished, and what it is poised to do going forward. I have seen it in action first-hand for more than 30 years. Know that our people are our strength, and they will never fail you or the nation. You are in good hands.”