This question already has an answer here:
At several points, ghosts are shown to be under the ministry’s control. But ghosts seem to care little about the law, and I doubt peeves would do it out of obedience. How does the ministry control ghosts? I am not asking how ghosts are punished, I am asking why ghosts are so easily controlled and ordered about, why don’t they refuse or run of? I repeat my earlier statement.