SAN JUAN, Puerto Rico — Puerto Rico Justice Secretary Wanda Vázquez has been sworn in as the island’s new governor, culminating weeks of political turmoil.

Vázquez took the oath of office early Wednesday evening at the Puerto Rican Supreme Court, which earlier in the day ruled that Pedro Pierluisi’s swearing in last week was unconstitutional.

The day’s events settle at least the dispute over who will lead the U.S. territory after its political establishment was knocked off balance by large protests.

But they were also expected to unleash a new wave of demonstrations because many Puerto Ricans have said they don’t want Vázquez as governor.

Former Gov. Ricardo Rosselló was forced to resign by anger over corruption, mismanagement of funds and a leaked obscenity-laced chat.

Pierluisi was sworn in as governor Friday. But that was challenged in court by the Senate.

The Puerto Rico Supreme Court on Wednesday said Pierluisi must step aside immediately.

“It is concluded that the swearing in as governor by Hon. Pedro R. Pierluisi Urrutia, named secretary of state in recess, is unconstitutional,” the court said in a brief statement.

After the ruling, Vázquez said she would step in as governor. At one point earlier, she had said she didn’t want the job.

“Puerto Rico needs assurance and stability. Our actions are aimed will be aimed toward that end and it will always come first,” she said in a statement.

Pierluisi said previously he would respect whatever ruling was made. Shortly after it was announced, someone yelled through a loudspeaker near the governor’s mansion: “Pierluisi out! The constitution of Puerto Rico should be respected!”

Pierluisi was appointed secretary of state by Rosselló while legislators were in recess, and only the House approved his nomination. Pierluisi was then sworn in as governor Friday after Rosselló formally resigned in response to angry street protests.

Puerto Rico’s Senate sued to challenge Pierluisi’s legitimacy as governor, arguing that its approval was also necessary, and the Supreme Court decided in favor of the Senate.

On Monday, the Senate decided not to hold a vote although the body’s president, Thomas Rivera Schatz, said Pierluisi had only five of 15 required votes. The same day the Supreme court announced it would hear the case.

The Senate had also asked the court to declare unconstitutional a portion of a 2005 law saying a secretary of state need not be approved by both House and Senate if they have to step in as governor. Puerto Rico’s constitution says a secretary of state has to be approved by both chambers.

The court agreed that the clause was unconstitutional.

Senate President Thomas Rivera Schatz praised the ruling in a triumphant statement.

“With absolute LEGITIMACY, we will seek TRUE PEACE and STABILITY,” Rivera Schatz said.

Six of its nine judges were appointed by governors from Pierluisi’s party, the pro-statehood New Progressive Party, and three by the main opposition Popular Democratic Party. The senator leading the challenge to Pierluisi is in his party.

Vázquez became justice secretary in January 2017 and has limited experience leading government agencies. She previously worked as a district attorney for two decades at Puerto Rico’s justice department, handling domestic and sexual abuse cases, and in 2010 was appointed director of the Office for Women’s Rights.

Some critics say that as justice secretary she was not aggressive enough in pursuing corruption investigations involving members of her New Progressive Party, and that she did not prioritize gender violence cases.

Last November, the Office of Government Ethics said it had received a complaint about possible ethical violations involving Vázquez, who was accused of intervening in a case involving a suspect charged with stealing government property at a home where Vázquez’s daughter lived.

Vázquez appeared in court to face charges including two violations of a government ethics law. In December a judge found there was no evidence to arrest her.

Rosselló’s resignation followed nearly two weeks of protests amid anger over corruption, mismanagement of funds and a leaked obscenity-laced chat in which he and 11 other men including government officials mocked women, gay people and victims of Hurricane Maria, among others.

More than two dozen officials resigned in the wake of the leak, including former Secretary of State Luis Rivera Marín.

“NOW is when that detestable group from the chat that lied, mocked, machinated, conspired, violated the law and betrayed Puerto Rico is truly ended and will leave government,” Rivera Schatz, the Senate president, said Wednesday.