BILLINGS, Mont. — President Trump on Thursday raised the prospect that he could face impeachment if Republicans lose control of Congress, imploring supporters at a campaign rally here to back GOP candidates in the midterm elections.

“You aren’t voting for a candidate, you’re voting for which party controls Congress. It’s a very important thing,” Trump said. “They like to use the impeach word. ‘Impeach Trump!’ . . . ‘But he hasn’t done anything wrong.’ ‘Doesn’t matter, we will impeach him!’ ”

The president’s riff came after days of mounting pressure on the White House over leaks from inside the administration that have deeply embarrassed the president over questions about his competence, raised by his own aides and political appointees.

In the wake of a critical new book from journalist Bob Woodward and an anonymous opinion essay in the New York Times, Trump has grown increasingly angry and paranoid over a feeling that his staff has betrayed him.

As he has repeatedly over the past two days, Trump again attacked the Times’s essay as “gutless” during his rally here and challenged the Times’s reporters to discover which senior official inside his administration was granted anonymity to write the essay.

“That would be a good scoop,” Trump told the crowd.

But it was his own future that was clearly on his mind.

“I say how do you impeach somebody who is doing a great job, hasn’t done anything wrong. Our economy is good. How do you do it?” Trump asked. “It’s a hell of a place in Washington.”

The rally — in support of GOP Senate candidate Matt ­Rosendale, Montana’s state auditor, who is challenging Democratic incumbent Jon Tester — was Trump’s first campaign event after Labor Day, as he enters an active stretch before the November midterms. White House aides have said the president will hold multiple campaign events most weeks before voters head to the polls Nov. 6.

Trump addressed the crowd, which held campaign signs and wore “Make America Great Again” hats, for more than an hour and recited his usual litany of complaints over what he views as unfair news coverage.

But Trump also seemed eager to defend his competence in the wake of the revelations in the book and the newspaper essay that his aides have allegedly taken action to hide information from him and ignore his instructions to set guardrails against his destructive impulses.

“Is he competent?” Trump said in a tone mocking a news anchor. “I think I’m pretty competent!”

Alluding obliquely to the New York Times op-ed, Trump said: “Look at that thing today. Is it subversion? Is it treason? The good thing about that is that even liberals that hate me think that’s a terrible thing they did.”

Although Trump rallies are typically boisterous, the crowd in Montana was restrained, and the president, who tends to feed off the energy of his audience, appeared at times to be going through the motions.

He attacked Democrats and liberal activists over their treatment of his Supreme Court nominee, Brett M. Kavanaugh, over three days of contentious Senate confirmation hearings.

“Do you believe the anger and the meanness on the other side?” Trump said. “It’s embarrassing to watch those people make fools of themselves screaming and shouting at this great gentleman.”

Of Democrats, he said: “Their whole campaign is resist, resist.”

Ahead of the rally, Trump conducted an offstage interview with a Fox News host, with the audio piped into the arena. The president criticized the Times for publishing the op-ed, saying the paper’s decision was “virtually treason.” Trump speculated that the author was probably a “low-level” or “deep state” person.

He did not indicate whether he had an idea who wrote the op-ed.

Trump also said he is not inclined to shut down the government over the border wall because Republican leaders do not want him to do so and have committed to taking up the issue after the midterm elections.

“I don’t want to do anything that’s going to hurt us or potentially hurt us,” Trump said.

It is up to him, he later added, after the crowd roared for a shutdown.

The rally was a meandering affair — maybe even more than usual. Sections of the arena were empty. The crowd was silent at times during his comments.

He gave a boost to a Florida Republican, recounted the 2016 campaign in detail, talked about his son loving to travel to Montana for hunting and said that even Abraham Lincoln faced the “fake news.”

Lincoln, he said, got poor reviews for his Gettysburg Address. But decades later, he said, the president that Trump has long admired was eventually given credit.

Nakamura reported from Washington.