The unanimous ruling ousted Pierluisi and paved the way for Wanda Vázquez, the secretary of justice, to take the oath of office as Puerto Rico’s third governor in five days.

With her husband and daughter by her side, Vázquez raised her right hand and was sworn in by Chief Justice Maite D. Oronoz Rodríguez at the Supreme Court in San Juan, the capital.

“Puerto Rico needs certainty and stability,” Vázquez, who had previously said she did not want the job, said in a statement before her swearing-in. She became the second female governor in Puerto Rican history.

The 29-page ruling said Pierluisi could not remain as governor, and called for “an orderly succession.”

Shortly before 5 p.m., Pierluisi said he would step aside. He had earlier left La Fortaleza, the governor’s official residence, in a black SUV with tinted windows.

“I want to be clear that the only motivation I have had during this time, as always, has been the well-being of Puerto Rico,” he said in a statement, in which he wished Vázquez well in her new role. “This is a time when we must all unite for Puerto Rico, leaving behind any partisan, ideological or personal agendas.”

The court ruled in favor of the Puerto Rico Senate, which sued late Sunday asking the court to issue a preliminary injunction against Pierluisi taking over the office of chief executive. He became governor Friday, even though he had not been confirmed as secretary of state by both chambers of the Legislative Assembly. Only the House of Representatives approved his recess appointment.

The events of the past few weeks represent the “most important juncture” in Puerto Rico’s history as a democracy, Oronoz wrote in a concurring opinion on Wednesday. “The summer of 2019 will be remembered as an unprecedented moment in which Puerto Ricans — of all ages, ideologies, backgrounds and creeds — threw themselves into the street to demand more from their government.”

Thomas Rivera Schatz, the Senate president who brought the legal challenge to Pierluisi’s appointment, said the court’s ruling showed it had been an illegal attempt by Puerto Rico’s ruling party to designate a successor outside the bounds of the constitution.

“To those who lent themselves, for their personal interest, to this embarrassing attempt to install an illegitimate government, that is how the history of Puerto Rico will remember you,” Schatz, who has been mentioned as a possible candidate for governor, said in a statement.

To justify Pierluisi’s ascent to the governor’s seat, Pierluisi and his predecessor, Ricardo Rosselló, cited a 2005 statute that said the secretary of state did not require legislative confirmation to step in as governor. On Wednesday, the court declared that portion of the law unconstitutional. The rest of the law, regarding the line of succession, is valid, the court found.

“The constitution requires the advice and consent of both chambers,” said Yanira Reyes Gil, a constitutional scholar and associate law professor at the Interamerican University of Puerto Rico. “It is a basic principle of constitutional law that the constitution takes precedence.”

In a concurring opinion, Justice Ángel Colón Pérez likened the way in which Rosselló handed over power to Pierluisi to the way a king turns over his throne.

“If a country’s ruler were empowered to choose his successor, or possible successor, without a minimal guarantee of democratic consensus, there would not be much difference between our system of government and a monarchy,” he wrote. “We do not live in a monarchy.”

Pierluisi’s lawyers had argued that the Senate could have voted on his nomination, as the House did, but chose not to do so before Rosselló’s resignation became effective Friday. Rosselló was forced out of office by massive public protests prompted by the leak of hundreds of pages of private messages in which he and his aides insulted politicians and everyday Puerto Ricans.

In a news conference Tuesday, Pierluisi, 60, told reporters that he would resign if the court ruled against him.

“The Supreme Court will decide,” he said. “I trust them, and they’ll do the right thing for Puerto Rico.”

Pierluisi was the shortest-serving governor in modern Puerto Rican history. Andrés González Muñoz, a Cuban military general who was named governor in 1898 while Puerto Rico was under Spanish rule, died the same day he arrived to take over the job.

After he was sworn in Friday, Pierluisi, a lawyer, said that while he consulted with Rosselló and Vázquez, he had made the decision himself to take the oath — despite the controversy that he knew existed over the 2005 statute, which had never been challenged in court.

“Who was going to stop me?” he said after being sworn in at his sister’s house.

Puerto Rico’s Constitution does not allow for the calling of early elections, though some lawmakers are now considering a constitutional amendment. Some 15 months remain on the four-year term started by Rosselló.

Pierluisi, like Rosselló, is a member of the ruling New Progressive Party, which supports statehood for Puerto Rico. He is also a Democrat when it comes to national politics, though political parties on the island do not match up with those on the mainland.

The national political affiliation of Vázquez, a career prosecutor, is unclear, if she even has one. Some of the protesters who marched on Rosselló later demonstrated against Vázquez.

In his brief time in office, Pierluisi, the island’s former nonvoting resident commissioner in Congress, emphasized his experience and competence. He made no major decisions and issued no executive orders but tried to give off an air of stability, holding meetings with agency heads and prominent businesspeople and discussing matters such as hurricane preparedness and the new school year.

But the political crisis has paralyzed much of the government for nearly a month and has hurt Puerto Rico’s credibility in Washington, where the Trump administration has already delayed more than $8 billion in federal disaster prevention funds pending more oversight.

Pierluisi said Tuesday that if forced out by the Supreme Court, he had no interest in remaining as secretary of state and would instead return to the private sector. If she does not wish to remain governor, Vázquez could appoint a new secretary of state and then resign.

If that were to occur, one possible candidate would be Jenniffer González-Colón, Puerto Rico’s resident commissioner in Congress, who already has been talked about as running for the seat in 2020. González-Colón is also a member of the ruling party, and her broad popularity could help the New Progressive Party hold on to power. But the crisis has revealed serious rifts among New Progressives, and protesters have made clear that they do not trust most institutions, especially not the party in government.

Sen. Eduardo Bhatia of the opposition Popular Democratic Party, which supports Puerto Rico’s commonwealth status, filed a court brief in support of the Senate’s lawsuit challenging Pierluisi’s appointment.

“They have a habit of doing things in a way that is not consistent with the law,” said Bhatia, one of several Popular Democratic candidates running for governor next year.

Kenneth McClintock, a former New Progressive secretary of state, said Pierluisi’s departure would not have a serious impact on governance.

“He was never a governor,” McClintock said. “He appeared to be a governor. He never was a governor. The swearing in was null.”

Even before he took office, Pierluisi struggled to overcome questions about the conflicts of interest he could face as governor. Lawmakers asked him whether he might some day find himself having to assert attorney-client privilege in matters relating to the fiscal control board, which was created by Congress to oversee Puerto Rico’s finances because of its more than $70 billion in public debt.

Court filings show that Pierluisi earned about $20,000 in legal work for the fiscal control board, according to the Public Accountability Initiative, a nonprofit group that conducts research on Puerto Rico.

The group found that he even billed the board for meetings with his former chief of staff, who now works for the control board. The president of the board is Pierluisi’s brother-in-law.

Pierluisi, the group said, did legal work for the board relating to the restructuring of the electric company’s debt while at the same time serving as a lobbyist for AES Corp., one of the electric company’s creditors.

Although many Puerto Ricans viewed Pierluisi as the least objectionable candidate from the ruling party, activists who led the movement on the streets to unseat the previous governor viewed his selection as an example of holding the seat inside the political establishment.

This article originally appeared in The New York Times.