(NEW YORK) — Michael Cohen, a former attorney for President Donald Trump, 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 soon-to-be-released tell-all book about his experiences with Trump.

Federal Judge Alvin Hellerstein sided with Cohen on Thursday.

“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, will be released Friday afternoon, according to his attorney, Danya Perry. 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 on Thursday. The Bureau 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.

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.

Cohen also was photographed by the New York Post dining outdoors in early July.

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.”

Perry told ABC News that the terms of Cohen’s home confinement are still being worked out, but she ruled out another return to federal prison.

“He was ordered to serve the remainder of his sentence on home confinement, so he should not have been remanded two weeks ago,” she said. “He should not be remanded again.”

Perry said Cohen plans to finish and publish his book.

Two other recent books about the president — “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 — have drawn legal challenges.

Both were published.

Copyright © 2020, ABC Audio. All rights reserved.