BRUSSELS — The head of Poland’s Supreme Court took a defiant stand Wednesday against a government effort to depose her, as thousands of supporters gathered to defend what they said was a last barrier preventing an authoritarian takeover of their country.

The president of the court, Malgorzata Gersdorf, showed up to work at the courthouse in Warsaw despite a law that went into effect at midnight forcing her retirement. The new measure escalated Poland’s confrontation with the European Union, whose leaders are already struggling to address boiling anti-migrant anger, the rise of Euroskeptic populists and President Trump’s efforts to drive wedges into the E.U. and the NATO defense alliance. 

The purge of Poland’s Supreme Court threatened to extend rollbacks in a nation that was once a post-communist democratic success story, critics said. Wednesday’s actions further aligned Warsaw with Hungary, whose leader has also slashed at pluralistic protections to entrench himself in power. 

The law that went into effect Wednesday completed the step-by-step government takeover of the judicial system, eliminating a final check on Polish leaders’ reshaping of their nation’s legal landscape. Proponents of the law said that their efforts were necessary to make courts more accountable to citizens. Tens of thousands of Poles turned into the streets on Tuesday night to protest the changes. 

“I stand here in defense of the legal order in Poland,” Gersdorf told supporters in front of the courthouse on Wednesday. Hundreds of people chanting “Constitution!” helped clear a path for her to enter the court in the morning. “I want to show that there’s a difference between the constitution and its violation,” she said. 

Later that day, thousands of people gathered at the court for a protest, where Solidarity leader Lech Walesa, who helped lead Poland out of communism, bitterly condemned the current government. 

“Whoever turns against the constitution, against the separation of powers, is a criminal,” he said. 

The European Union on Monday launched legal action against Polish leaders, saying that the new law dealt a blow to judicial independence. Opposition leaders in Poland were pinning their hopes on that intervention, although any move by European courts is likely to take months. Inside Poland, the constitutional court, the main check on the constitutionality of new laws, is already controlled by Polish leaders, and government critics said the court was unlikely to step in. 

The ruling Law and Justice party also has control over lower courts. Wednesday’s change awards it power at the top court, which approves election results and handles appeals for civil and criminal cases. 

“We have reinforced judicial independence and objectivity,” Polish Prime Minister Mateusz Morawiecki defiantly told the European Parliament in Strasbourg, France, on Wednesday, where lawmakers heckled and booed. “Democracy in Poland has never been as alive as it is today.” 

Polish leaders have said they are trying to overhaul the judicial system to purge it of judges who began their careers before the communist government fell in 1989. They say those judges are relics of the old system and are blocking the will of the people as expressed by the current leadership, which was elected in 2015. 

The leader of the Law and Justice party, Jaroslaw Kaczynski, grew skeptical of the courts after they blocked a number of his party’s initiatives during a previous stint in power. 

In Strasbourg, Morawiecki came under withering criticism. 

“Turn the wheel and bring Poland back into the family of democratic nations,” said Guy Verhofstadt, a senior European lawmaker. 

The new law lowers the retirement age of the Supreme Court from 70 to 65, forcing out about a third of its members. Judges can apply to Polish President Andrzej Duda to extend their terms, but Gersdorf, 65, refused to do so. Her term was supposed to run until 2020. She said she believes that the law was designed to force her from office. 

Polish media reported that the court’s press office said Wednesday that 63 judges were working, suggesting that 10 had accepted their retirement, but that only 55 of the judges would actually be assigned new cases because eight had not submitted a request to extend their terms. 

The Polish leadership appeared to be trying to sap some of the energy from the protests by allowing Gersdorf to preside over an administrative meeting of judges on Wednesday. Nor has Duda signed the papers formally required to enforce a judge’s retirement. And he has held off from naming a new president of the court. 

But protest leaders said the damage was already done. 

The law “is really dangerous,” said Borys Budka, a former justice minister who is now an opposition lawmaker. “The only way to block this very bad situation in Poland from a legal outlook is civil resistance and European and international opinion.”

Magdalena Foremska in Wroclaw, Poland, contributed to this report.