NEW YORK — A federal judge on Thursday ordered President Trump's ex-lawyer Michael Cohen released from prison to home confinement, saying the Justice Department's move to take him back into custody earlier this month was retaliation for writing a book about his former boss.

U.S. District Judge Alvin Hellerstein said Cohen must be released by 2 p.m. Friday, after he is tested for the novel coronavirus. He has been held in a solitary setting at the federal prison in Otisville, N.Y., since being rearrested July 9.

Before he was abruptly retaken into custody, Cohen had been on furlough in Manhattan since May, part of a compassionate-release program established as the coronavirus swept through jails and prisons nationwide. Cohen and his lawyers alleged his detention stemmed from plans to write an unflattering tell-all about the president and Cohen’s experience in the justice system — a memoir, he said, that would cite examples of Trump making racist and anti-Semitic comments in private settings. They argued that a gag order outlining the conditions for his continued release — barring him not only from writing the book but also muzzling him from even discussing it — was tantamount to a violation of his constitutional rights.

The judge agreed.

“I make the finding that the purpose of transferring Mr. Cohen from furlough and home confinement to jail is retaliatory, and it’s retaliatory because of his desire to exercise his First Amendment rights to publish a book and to discuss anything about the book or anything else he wants on social media and with others,” Hellerstein said in a scathing ruling issued Thursday morning. “In 21 years of being a judge and sentencing people and looking at the terms and conditions of supervised release, I have never seen such a clause.”

In a statement Thursday evening, Bureau of Prisons officials called such an assertion “patently false” and described the process by which Cohen was initially approved for compassionate release. The statement, which was unattributed, said that Cohen sought to dictate the terms of his monitoring while on home confinement and ultimately refused to sign his probation form.

“While it is not uncommon for BOP to place certain restrictions on inmates’ contact with the media,” the statement says, “Mr. Cohen’s refusal to agree to those conditions here played no role whatsoever in the decision to remand him to secure custody nor did his intent to publish a book.”

The BOP official who ordered Cohen returned to prison, Jon Gustin, told the court in a sworn statement earlier this week that he was unaware Cohen was writing a book and that he had conferred with two members of the executive staff before making the decision.

In his ruling, Hellerstein was fiercely critical of the defense put forth by the Justice Department, which had argued that Cohen was “combative” in a discussion about the house-arrest agreement. A probation officer, Adam Pakula, said in an affidavit that he was unaware Cohen was writing a book at the time he gave him the agreement.

Pakula said Cohen took issue with other terms detailed on the form, including that preapproval would be needed for future employment and that he would be barred from communicating with fellow convicted felons. Cohen also objected to being told he could not do his own grocery shopping, the officer said.

Pakula said he conveyed Cohen’s concerns and behavior to Bureau of Prisons officials, who determined he was to be remanded.

Throughout Thursday’s proceeding, Hellerstein pressed a lawyer for the Manhattan U.S. attorney’s office who had argued that Cohen was “antagonistic” during the meeting and, thus, rightly sent back to prison.

Assistant U.S. Attorney Allison Rovner argued that Cohen’s lawyer Jeffrey Levine tried to haggle with the probation officer on his requirement that Cohen wear an ankle monitor, saying it was a condition reserved for “violent” convicts. There was nothing wrong with Levine trying to negotiate for his client, the judge said, and the attempt to debate the terms should not have been cause to imprison him.

“What’s an attorney for if he is not going to negotiate an agreement with his client?” the judge said.

A spokesman for the U.S. attorney’s office declined to comment on whether prosecutors intend to appeal.

Danya Perry, one of Cohen’s attorneys, called the ruling a victory for the First Amendment. “This principle transcends politics and we are gratified that the rule of law prevails,” Perry said.

Cohen’s legal team also includes lawyers from the American Civil Liberties Union.

Cohen was convicted of tax evasion, making false statements, campaign finance violations and lying to Congress in 2018. He admitted to making hush-money payments during the 2016 presidential campaign to two women who alleged having affairs with Trump several years ago. The president has denied their claims.

Cohen’s three-year sentence expires in November 2021.