By AARON KATERSKY and IVAN PEREIRA, ABC News

(NEW YORK) — Michael Cohen, President Donald Trump’s former attorney, will be released from federal prison and return to home confinement in Manhattan.

Cohen, serving a three-year sentence for violating campaign finance laws and lying to Congress, was ordered back to the federal facility in Otisville, New York, on July 9 after spending two months on furlough in home confinement. The Federal Bureau of Prisons said Cohen didn’t agree to a clause in his home confinement agreement that forbade him from speaking with the media, including through a book.

Cohen contended in a lawsuit that he was being retaliated against by the federal government, which objected to a tell-all book on his experiences with Trump that’s slated to be released soon.

Federal Judge Alvin Hellerstein sided with Cohen on Thursday and ordered he be released.

“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 and to publish a book,” Hellerstein said.

Cohen, who had been in solitary confinement, according to his lawsuit, must be released by Friday afternoon, the judge ordered. Hellerstein also said the conditions of Cohen’s home confinement will be renegotiated.

The Bureau of Prisons didn’t immediately return requests for comment from ABC News.

Cohen was one of several inmates released from federal prison in May after the Department of Justice raised concerns about COVID-19 in facilities. He was active on social media and teased the release of his book, which he said would include anecdotes of Trump using anti-Semitic and racist remarks. He also was photographed by the New York Post dining outdoors in early July.

The Bureau of Prisons previously said Cohen turned down the offer to remain at home in exchange for the media gag order, but Cohen contends he never made such an agreement and was taken into custody before he and his lawyer could negotiate terms.

Hellerstein blasted the federal government for issuing new guidelines to Cohen’s furlough and home confinement.

“How can I take any other inference but that it was retaliatory?” Hellerstein asked rhetorically during a hearing. “In 21 years of being a judge … I’ve never seen such a clause.”

“He was put on furlough with no conditions other than hang around your house and be in your neighborhood,” the judge added. “All of a sudden when the New York Post article comes out and the Bureau of Prisons understands he’s writing a book, and it’ll be finished by election time, they impose conditions.”

Danya Perry, Cohen’s attorney, said he would abide by the conditions laid out by the Bureau of Prisons once the requirement about media engagement was renegotiated.

“He wants to be able to edit and publish his book,” she said.

Two other recent books about the president have drawn legal challenges: The Room Where it Happened, by former national security adviser John Bolton, and Too Much and Never Enough, by Mary Trump, the president’s niece.

Both were published.

Copyright © 2020, ABC Audio. All rights reserved.