A D.C. Superior Court judge on Friday ordered the city to move people out of makeshift shelters for homeless families, saying the city’s communal shelters of last resort appear to be denying children needed privacy and security and could be emotionally traumatizing.

In a temporary restraining order, Judge Robert S. Tignor told the District government to immediately begin removing families from two recreation centers and start relocating them into private rooms with insulated walls, locking doors and lights that can be turned off at night.

A class-action lawsuit filed on behalf of more than 50 families placed in the two makeshift shelters since late January said children, parents and sometimes grandparents had been unable to shower for days and got only cots in big, noisy rooms, illuminated all night. Flimsy partitions exposed unrelated families to one another.

“The court finds that they, particularly the children, incur increased risk of communicable disease, are denied adequate privacy and physical security, are likely to experience emotional trauma and stress, hence are likely to suffer irreparable harm in the absence of a restraining order,” Tignor wrote.

Dora Taylor, a spokeswoman for the D.C. Department of Human Services, said the agency interpreted the ruling as applying only to the members of the families who were named as plaintiffs in the class-action lawsuit and who were still in shelters.

Taylor said the agency moved them into hotels Friday night, fully complying with the ruling.

Four families were named plaintiffs in the lawsuit, which was brought on behalf of all similarly situated families. The judge’s order only said plaintiffs “are individuals entitled to severe weather shelter.”

Taylor said the city would continue to make its case. The next hearing is scheduled for March 21.

The number of homeless families seeking emergency shelter has more than doubled this winter. The city’s only shelter for children was filled with 300 families at winter’s start. As families continued to arrive, the District had to rent more than 400 motel rooms in the District and Maryland.

In January, Maryland officials objected to taking any more, and city officials said they had to take the unprecedented step of putting families in common rooms of city recreation centers.

The lead plaintiff, Melvern Reid, who has custody of her 10-year-old grandson, described two nights in one of the shelters in which the two could barely sleep and the boy was “terrified. He was scared of the strangers in the room with him and did not want to leave his grandmother’s side,” according to the complaint.

The filing said the boy slept in his clothes, fearful to change in front of others, had to be left with a stranger while Reid, 59, used the bathroom, and slept with a blanket over his head because of the lights. He also repeatedly went to school without a shower because the families were not allowed to use the showers at the recreation center, according to the filing.

“We and our clients are very relieved” by the judge’s order, said Allison Holt, a Hogan Lovells attorney who brought the case.

After emergency hearings on t he request this week on whether to issue the temporary restraining order, Tignor granted it Friday night, saying it was “likely, though not certain” that the plaintiffs would prevail in arguing that the makeshift shelters did not comply with city law.

Tignor said the continued demand was “apparently caused in part by an unusually cold winter.” He wrote that he found the city’s argument that the communal shelters met the letter of the law for apartment-style housing for families “without merit.”

The city’s primary shelter for homeless families is the shuttered D.C. General Hospital campus, where relatives share rooms.

Mayor Vincent C. Gray’s administration has said that the attractiveness of the motel units, mostly efficiencies, has drawn families who might have been able to find private accommodations. As of Thursday, 23 families, nearly 80 people, were in the recreation centers.