A federal judge on Wednesday halted President Donald Trump's executive order that gave state and local officials the ability to shut the door on refugees, and ignited a fierce debate in communities about how welcoming the United States should be.

U.S. District Judge Peter Messitte in Maryland said in his ruling that the president's order “flies in the face of clear Congressional intent" of the 1980 Refugee Act by allowing state and local governments to block the resettlement of refugees in their jurisdictions.

In issuing a preliminary injunction, Messitte said the process should continue as it has for nearly 40 years, with refugee resettlement agencies deciding where a person would best thrive.

Church World Service, Lutheran Immigration and Refugee Service, and HIAS — a Jewish nonprofit — filed the lawsuit in Greenbelt, Maryland, on Nov. 21. They said they already work closely with state and local officials before resettling refugees in an area.

They called the order an attempt at a state-by-state ban on refugees.

Messitte agreed. “It grants them veto power. Period,” the judge wrote.

The Trump order, which was issued in September and had been set to go into effect in June, required agencies to get written consent from state and local officials before resettling refugees in their jurisdictions. Trump said he acted to respect communities that believe they do not have the jobs or other resources to be able to take in refugees.

The White House said in a statement that the ruling was “preposterous"and that Congress under the Refugee Act afforded the president authority over the refugee resettlement process.

“Another lawless district court has asserted its own preferred immigration policy in place of the laws of the United States — and, in so doing, robbed millions of American citizens of their voice and their say in a vital issue directly affecting their communities,” the statement said.

The order was the latest attempt by the Trump administration to thrust states and cities into immigration policy. It caused heated debates and raucous meetings in several towns from North Dakota to Tennessee, and put Republican governors in an uncomfortable position between immigration hardliners who want to shut the door and some Christian evangelicals who believe helping refugees is a moral obligation. Others say refugees are vital to fill jobs and keep rural communities afloat.

Trump has brought the order up on the campaign trail, getting big applause from supporters at a Minneapolis rally in October when he said: “As you know, for many years leaders in Washington brought large numbers of refuges to your state from Somalia, without considering the impact to schools and communities and taxpayers. I promise you that as a president I will give communities local control."

The ruling comes less than a week after Texas became the first state to say it did not want more refugees.

Gov. Greg Abbott said in a letter released Jan. 10 that Texas, which took in more refugees than any other state during the 2018 fiscal year, “has been left by Congress to deal with disproportionate migration issues resulting from a broken federal immigration system.” Abbott's office did not immediately respond to messages seeking comment on Wednesday’s ruling.

Refugees have the right to move anywhere in the U.S. after their initial resettlement, but at their own expense.

The head of Lutheran Immigration and Refugee Service, Krish O’Mara Vignarajah, said Trump's order was already causing “irreparable harm to refugee families." Agencies were forced to dedicate time and resources to getting written consents instead of focusing on efforts to help refugees integrate with language classes and job training.

Agencies have gotten consents from 42 states.

Mark Hetfield, the head of HIAS, estimated about 1,800 refugees this year will be resettled in Texas, where most have family members already, unless the Trump administration appeals the ruling and wins. Mayors from the state's major cities have welcomed them.

Ali Al Sudani, chief programs officer for Interfaith Ministries for Greater Houston, said many refugees were anxious after Abbott’s announcement. One Iraqi man contacted Al Sudani to say that he was in the process of being approved to go to Texas and asked whether he should request another state instead.

Al Sudani called Wednesday's ruling “great news."

“But we don’t want this to polarize different communities even further,” he said. “I hope and wish that everyone would come and see the fact that this is the right thing to do.”

The Trump administration has capped the number of refugee admissions at 18,000 for the current fiscal year. About 30,000 refugees were resettled in the U.S. during the past fiscal year; between 150,000 and 200,000 remain in the pipeline for possible U.S. resettlement while they live abroad, according to plaintiffs' attorney Linda Evarts, of the International Refugee Assistance Project.

Associated Press reporter Nomaan Merchant in Houston contributed to this report.

CORRECTION (Jan. 15, 2020, 5:39 p.m. ET): This version corrects that Trump issued the order in September, not November.