It is legal until you inform them they are trespassing and ask them to leave.
https://www.law.cornell.edu/wex/trespass
So it's illegal to, for example, go door to door looking for one that somebody forgot to lock and then spend the night there.
Beyond that, it's only unlawful.
100% legal (castle doctrine) to shoot them, think about that for a minute, not generally legal to shoot someone engaging in a legal activity.
--edit--
Also legal to shoot them through the door but probably not such a good plan...
An intruder must be making (or have made) an attempt to unlawfully or forcibly enter an occupied
residence, business, or vehicle.
The intruder must be acting unlawfully (the castle doctrine does not allow a right to use force
against officers of the law, acting in the course of their legal duties).
The occupant(s) of the home must reasonably believe the intruder intends to inflict serious bodily
harm or death upon an occupant of the home. Some states apply the Castle Doctrine if the occupant(s) of the home
reasonably believe the intruder intends to commit a lesser felony such as arson or burglary.
The occupant(s) of the home must not have provoked or instigated an intrusion; or, provoked/instigated
an intruder's threat or use of deadly force.
https://en.wikipedia.org/wiki/Castle_doctrine#Conditions_of_...Here, unless you give them a reason they're just like "yeah, dude opened the wrong door, heh?"