John S. Pistole, who transformed the Transportation Security Administration from a picket-line defense against terrorists to one that ferreted out information about potential threats, announced Thursday that he will retire from the agency at year’s end.

More than four years ago, the White House begged the lifelong FBI agent to take the job that nobody wanted as TSA director, a thankless task that promised routine public scoldings on Capitol Hill and waves of resentment from Americans who found unhappiness with the blue-suited TSA airport officers.

Pistole got that, and more.

A Congress that could agree on little else seemed almost gleeful in calling him in for chastisement before one committee after another.

Rep. Jason Chaffetz (R-Utah) bemoaned the “mistreatment of an innocent American at the hands of TSA.”

Rep. John L. Mica (R-Fla.) cited the full-body scanner debacle of 2013 as “another very bad decision by TSA . . . and the taxpayers are going to take it on the chin.”

Rep. Mark Sanford (R-S.C.) demanded that Pistole explain “how do [you] get into somebody else’s head? I think it’s a very difficult place to be.”

Through it all, Pistole sat at the witness table, responding calmly but refusing to be ruffled or roughed up.

Sen. Barbara Boxer (D-Calif.), who does not hide her passion under a barrel, once snapped that Pistole was “too calm.”

“I try not to be too over the top, to be measured in what I do,” Pistole said.

“This is serious business,” Boxer said.

“I agree.”

“I don’t think you’re taking this seriously,” Boxer said.

“I disagree.”

“I still like you,” Boxer said.

“I like you, too,” Pistole said. “We disagree.”

Throughout all the turmoil before Congress and public fuss about body scanners that revealed too much or “full-body pat-downs,” Pistole saw his mission as taking the TSA from an agency that stood guard in airport terminals to one that figured out in advance who the bad guys were and how to thwart them.

Pistole’s day began with an 8:30 a.m. intelligence briefing in a windowless room at TSA’s Arlington headquarters, seated at the head of a long table flanked by more than a dozen key intelligence officers with others beamed in on big television screens that lined the walls.

The four digital clocks reflected the scope of TSA’s reach: Frankfurt, Germany; Bangkok; Karachi, Pakistan; and Washington.

Combed from millions of passenger records worldwide, the briefing took a 72-hour look ahead at who planned to fly into the United States.

“The intelligence we have on the front end exceeds anybody in the world,” Pistole said.

As the focus shifted to bad guys, the TSA also sought to make travel less arduous for good guys — primarily American citizens who posed no threat. Creating the TSA PreCheck program and security exemptions for several other categories, TSA says that more than half of all passengers in the United States are eligible for some form of expedited passage through airport security.

Waiting times at checkpoints also have been reduced.

“Shifting TSA from a one-size-fits-all approach to risk-based security has empowered the agency to provide the most effective security in the most efficient ways,” Sen. Jay Rockefeller (D-W.Va.) said Thursday.

Sen. Tom Carper (D-Del.) said Pistole “modernized a number of agency policies to enhance the security of the traveling public while making air travel more convenient.”

Pistole, 58, is in line to become president of Anderson University, the Indiana institution from which he earned a bachelor’s degree in 1978. The school’s board is to vote on his nomination Oct. 27.