President Trump aired his grievances against the news media, the intelligence community and his detractors in a sprawling stream-of-consciousness news conference Thursday, capping an extraordinary four weeks in office marked by tumult, disarray and infighting.

The beleaguered chief executive defended his advisers against claims of improper contacts with Russia and claimed — contrary to widespread perceptions both inside and outside the White House — that his fledgling administration “is running like a fine-tuned machine.”

“To be honest, I inherited a mess,” he said in a news conference that lasted an hour and 17 minutes and was, by turns, rambling, combative and pure Trump. “It’s a mess. At home and abroad, a mess.”

Yet moments later, the president seemed to acknowledge the widespread reports of turbulence and upheaval emanating out of the West Wing, only to claim that his White House — which so far has been marred by staff feuding, a controversial travel ban, false statements and myriad leaks — was operating seamlessly.

“I turn on the TV, open the newspapers and I see stories of chaos — chaos,” he said. “Yet it is the exact opposite. This administration is running like a fine-tuned machine, despite the fact that I can’t get my Cabinet approved.”

President Trump speaks during a news conference in the East Room of the White House on Thursday. (Jabin Botsford/The Washington Post)

Trump’s news conference — with the president firmly at the center as both complainer and defender in chief — capped a month of turmoil in what so far is the most tumultuous start to any U.S. presidency in modern history. His approval ratings are underwater in most polls, and he is battling setbacks including the ouster on Monday of national security adviser Michael Flynn and the decision Wednesday by his nominee for labor secretary, Andrew Puzder, to withdraw amid mounting opposition on Capitol Hill.

The turmoil continued Thursday evening as Trump’s pick to replace Flynn, retired Vice Adm. Robert Harward, turned down the job, according to people familiar with the offer.

[In an erratic performance, President Trump shows his supporters who’s boss]

A senior U.S. official said that “family considerations changed his mind,” and a friend of ­Harward’s added that the ­hard-charging former Navy SEAL was not fully comfortable with the quickly moving process. One factor in Harward’s decision was that he could not get a guarantee that he could select his own staff, according to a person close to Trump with knowledge of the discussions.

Trump had said earlier at the news conference that one of the reasons he felt that he could let Flynn go was because he had a good replacement in mind, without naming that person. “I have somebody that I think will be outstanding for the position,” he said. “And that also helps, I think, in the making of my decision.”

Asked about recent reports in The Washington Post that Flynn had improperly discussed Russian sanctions with the country’s ambassador to the United States before Trump was sworn in, the president defended Flynn as a “fine person,” saying he had done nothing wrong in engaging the Russian envoy.

But Trump said that Flynn had erred by misleading government officials, including Vice President Pence, about his conversations with Russia, which is why he ultimately demanded his resignation.

“He didn’t tell the vice president of the United States the facts,” Trump said. “And then he didn’t remember. And that just wasn’t acceptable to me.”

Trump also made clear that he had no problem with Flynn discussing with the Russian ambassador the sanctions imposed on Moscow by the Obama administration, saying it was Flynn’s job to reach out to foreign officials.

“No, I didn’t direct him, but I would have directed him if he didn’t do it,” Trump said.

[Fact-checking President Trump’s news conference]

Asked several times about reports in the New York Times and on CNN that his campaign had repeated contacts with Russia, including senior intelligence officials, Trump grew testy as reporters pushed him for a yes or no answer.

He said that he personally had not had contact and that he was not aware of such contacts during the campaign.

“Russia is a ruse,” Trump said. “I have nothing to do with Russia. Haven’t made a phone call to Russia in years. Don’t speak to people from Russia. Not that I wouldn’t. I just have nobody to speak to.”

Trump’s general defense of Russia stood in contrast to comments that Defense Secretary Jim Mattis made at a NATO meeting Thursday in Brussels, where he said that there was “very little doubt” that the Russians have either interfered or attempted to interfere with elections in democratic nations.

Thursday’s news conference was ostensibly billed as a chance for Trump to announce Alexander Acosta as his new nominee for labor secretary. If confirmed, Acosta would be the first Latino in Trump’s Cabinet.

But for 77 minutes, the president offered the verbal equivalent of the brash and impetuous early-morning tweets that have become the alarm clock for much of Washington. He took aim at topics including the recent controversies over Russia, which he dismissed, and the “criminal leaks” within the intelligence community. Although he inherited a growing economy, low inflation and low unemployment, he repeatedly portrayed a country in shambles under President Barack Obama.

Trump also said he would use his remarks to bypass the “dishonest media” and speak directly to the American people about the “incredible progress” his administration has made.

The president began on a subdued, almost melancholy note, looking down repeatedly to read from prepared remarks on his lectern. But he became more fiery and animated — joyful, even — when he began to banter and joust with the assembled reporters.

[Trump’s Labor pick, a former Justice official, would be first Hispanic in his Cabinet]

He reprised some of his favorite themes from the campaign trail, complaining about Hillary Clinton, whom he referenced 12 times; criticizing Obama’s policies, from his Affordable Care Act to his failed reset with Russia; and relitigating wounds old and new, in a Festivus-caliber airing of grievances.

And he boasted of his accomplishments so far. “I don’t think there’s ever been a president elected who in this short period of time has done what we’ve done,” Trump said.

He said he has asked the Justice Department to look into the leaks coming out of U.S. intelligence agencies. He promised a new executive order by the middle of next week that would replace the now-frozen directive that temporarily barred refugees and citizens of ­seven Muslim-majority countries from entering the United States. Trump also said he would put forward a plan to repeal Obama’s Affordable Care Act by mid-March, with a tax reform package soon after.

“Tax reform is going to happen fairly quickly,” he said. “We’re doing Obamacare. We’re in the final stages.”

Trump repeatedly lambasted the “fake news” media — which at one point he upgraded (or downgraded) to the “very fake news” media — while promoting some dubious claims and fake news of his own.

Trump was pressed on his incorrect assertion that he had the largest margin of victory in the electoral college since President Ronald Reagan, when Obama, Bill Clinton and George H.W. Bush had bested him in all of their victories. The new president blamed faulty facts.

“I was given that information,” he said. “Well, I don’t know, I was given that information.”

During the news conference, Trump alternated between showering the media with scorn and adopting a more playful, almost jaunty, tone. At one point, he insisted that he was enjoying himself.

“I’m not ranting and raving — I love this,” he said. “I’m having a good time doing this.”

[Trump’s combative, complaint-filled news conference, annotated]

In an exchange with April Ryan of American Urban Radio Networks — the only black reporter called on by Trump — the president asked her to arrange a meeting with the Congressional Black Caucus.

“Do you want to set up the meeting? Are they friends of yours?” he asked.

Trump also claimed that he had tried to meet with Rep. Elijah E. Cummings (D-Md.), a prominent member of the group, but that Senate Minority Leader Charles E. Schumer (D-N.Y.), whom he called a “lightweight,” had urged Cummings not to attend.

In a statement, Cummings rebutted Trump’s version of the facts. “I have no idea why President Trump would make up a story about me like he did today,” he said. “Of course, Senator Schumer never told me to skip a meeting with the President.”

In another notable exchange with a Jewish reporter, who asked what Trump was going to do to tamp down on the uptick in anti-Semitism in the country since he took office, the president rejected the idea that he or his rhetoric might be partially to blame.

“Number one, I am the least anti-Semitic person that you’ve ever seen in your entire life,” Trump said. “Number two, racism, the least racist person.”

Trump’s Thursday performance seemed an acknowledgment, by the president, that he may be his own best press secretary and senior adviser, and allowed him to appear both confident, comfortable and in control.

While many of his comments, as well as the sometimes disjointed nature of his delivery, are certain to alarm official Washington, they are also the sorts of red-meat talking points that delighted his base during the campaign and helped propel him to victory.

“I won with news conferences and probably speeches,” he told the assembled reporters. “I certainly didn’t win by people listening to you people.”

Robert Costa, Adam Entous and Jenna Johnson contributed to this report.