I used to have this problem at a barn i worked at; we had 4 horses in the back pasture and none of them were in work at the time. We used to open up the door and open each stall and let them run or down the passage, then to let them in we used to open the door and they'd all barrel in pushing and shoving. Dangerous. We'd end up with two in one stall fighting like idiots.
So i took over letting them out and in. I'd open each stall one by one. the farthest away first, they'd mostly walk out if there was no one else with them. to let them in i'd squeeze through the door gap and call them over; they'd come running and find me and a closed door. I'd have to single one out and keep the rest back (i carried a short whip in case they got too close and too pushy); i'd open the door, let one through then close the door behind me. I'd make sure each one was secure in their stall before i let the another one in. It worked pretty well.
It got to the point where they each knew where to go and would walk in calmly one behind the other, find their stall and go in. i'd just walk up behind them and close each door.
Now my horse has moved and is brought in from his field with a buddy. He knows to stand facing away from his food in his stall so his halter can be taken off before he eats. If he pulls or gets obnoxious he has to wait a few minutes before i'll take his halter off so he can eat.