Washington (CNN) A federal judge ruled against the Justice Department on Friday in a lawsuit over withholding federal money from so-called sanctuary cities, the latest blow to the Trump administration's hardline immigration tactics.

The lawsuit challenged the Justice Department's efforts to punish sanctuary cities by withholding a key law enforcement grant the department said was available only to cities that complied with specific immigration enforcement measures.

In July 2017, then-Attorney General Jeff Sessions announced that applicants for Edward Byrne Memorial Justice Assistance Grants would have to comply with federal immigration enforcement in ways that were unlike years past, like allowing federal law enforcement agents to have access to detainees in jails for questioning about their immigration status.

According to the ruling, the seven states involved in the lawsuit, as well as New York City, had been receiving the grant money since Congress created the fund for the "modern version of the program in 2006," and the funds "collectively totaled over $25 million."

"In 2017, for the first time in the history of the program, the U.S. Department of Justice ('DOJ') and Attorney General (collectively, 'Defendants') imposed three immigration-related conditions that grantees must comply with in order to receive funding," wrote Judge Edgardo Ramos, of the US District Court for the Southern District of New York, in his ruling.

Read More