NEW YORK -- A judge ordered the release from prison of U.S. President Donald Trump's former personal lawyer on Thursday, saying the government retaliated against him for planning to release a book critical of Trump before November's election.

Michael Cohen's First Amendment rights were violated when he was ordered back to prison on July 9 after probation authorities said he refused to sign a form banning him from publishing the book or communicating publicly in other manners, U.S. District Judge Alvin K. Hellerstein said during a telephone conference.

Hellerstein ordered Michael Cohen released from prison to home confinement by 2 p.m. on Friday.

"How can I take any other inference than that it's retaliatory?" Hellerstein asked prosecutors, who insisted in court papers and again Thursday that Probation Department officers did not know about the book when they wrote a provision of home confinement that severely restricted Cohen's public communications.

"I've never seen such a clause in 21 years of being a judge and sentencing people and looking at terms of supervised release," the judge said. "Why would the Bureau of Prisons ask for something like this ... unless there was a retaliatory purpose?"

In ruling, Hellerstein said he made the "finding that the purpose of transferring Mr. Cohen from furlough and home confinement to jail is retaliatory." He added: "And it's retaliatory for his desire to exercise his First Amendment rights to publish the book."

Cohen, 53, sued federal prison officials and Attorney General William Barr on Monday, saying he was ordered back to prison because he was writing a book: "Disloyal: The True Story of Michael Cohen, Former Personal Attorney to President Donald J. Trump."

In a written declaration, Cohen said his book "will provide graphic and unflattering details about the President's behaviour behind closed doors," including a description of anti-Semitic and "virulently racist remarks" against Black leaders including President Barack Obama and Nelson Mandela, South Africa's first Black president.

He said he worked openly on his manuscript until May at Otisville's prison library and discussed his book with prison officials. He said he was told in April that a lawyer for the Trump Organization, where he worked for a decade, was claiming he was barred from publishing his book by a non-disclosure agreement. Cohen disputes that.

Cohen has been in isolation at an Otisville, New York, prison camp, quarantined while prison authorities ensure he does not have the coronavirus.

Prosecutors declined through a spokesperson to comment on Hellerstein's ruling.

Cohen's attorney, Danya Perry, said in a statement that Hellerstein's order was "a victory for the First Amendment" and showed that the government cannot block a book critical of the president as a condition of release to home confinement. "This principle transcends politics and we are gratified that the rule of law prevails," she said.

Cohen was released in May along with other prisoners as authorities tried to slow the spread of the COVID-19 in federal prisons.

He was one year into a three-year prison sentence after pleading guilty to campaign finance charges and lying to Congress, among other crimes.

Campaign finance charges related to his efforts to arrange payouts during the 2016 presidential race to keep the porn actress Stormy Daniels and model Karen McDougal from making public claims of extramarital affairs with Trump. Trump has denied the affairs.