Bowe Bergdahl, the US army sergeant whom Donald Trump called a traitor who would have been executed in “the old days”, told a military judge on Monday he was pleading guilty to desertion and misbehavior before the enemy.

In 2009 Bergdahl, now 31, walked away from a remote post in Afghanistan. He was held prisoner until 2014, when an exchange for five Taliban prisoners was agreed. On Monday morning he told the judge, Col Jeffery R Nance, that he now understands that what he did caused others to search for him.

Serious wounds to service members who searched for Bergdahl are expected to be considered in his sentencing hearing, which will take place later this month.

Amid Republican criticism of the Obama administration for agreeing to the prisoner swap – which the then president answered by saying the US does not leave any service member behind – Trump harshly criticized Bergdahl.

At campaign rallies, Trump repeatedly called the soldier a traitor and said he would have been executed in the “old days”. In a July 2016 speech in Indiana, Trump lamented that Bergdahl could wind up with a light punishment.

“Remember the old days? A deserter, what happened?” he said, before miming pulling a trigger and adding: “Bang.”

Bergdahl’s defense team was rebuffed in an effort to prove Trump had unfairly swayed the case. A request for a pardon from Obama was not successful.

In a February ruling, the judge ruled that Trump’s campaign-trail comments were “disturbing and disappointing” but did not constitute unlawful command influence by the soon-to-be commander in chief.

“We may as well go back to kangaroo courts and lynch mobs that got what they wanted,” Bergdahl said in a 2016 interview with a British filmmaker obtained by ABC News/Good Morning America and aired on Monday. “The people who want to hang me, you’re never going to convince those people.”

Bergdahl’s lawyer said on Monday the prosecution and defense had not agreed to a stipulation of facts in the case, an indication they did not reach a deal to limit punishment. The misbehavior charge carries a maximum penalty of life in prison while the desertion charge is punishable by up to five years.

Bergdahl’s five years of captivity will probably play a role in determining what punishment he receives. The native of Hailey, Idaho, chose to have his case heard by a judge alone, rather than a jury.

Legal scholars have said that several pre-trial rulings against the defense have given prosecutors leverage to pursue stiff punishment. Perhaps most significant was the judge’s decision in June to allow evidence of serious wounds to service members who searched for Bergdahl at the sentencing phase.

The judge ruled that a navy Seal and an army national guard sergeant would not have wound up in separate gun battles that left them wounded if they had not been searching for Bergdahl.

Defense attorneys have acknowledged that Bergdahl walked off his base without authorization. Bergdahl told a general in a preliminary investigation that he left intending to cause alarm and draw attention to what he saw as problems with his unit.

His defense team has argued that he cannot be held responsible for a long chain of events that included decisions by others on how to conduct the searches.

Bergdahl has been assigned to desk duty at a Texas army base while his case unfolds.