how would it concentrate that beam and prevent it from dispersing once it left the barrel?
It wouldn't. Plasma is extremely slippery stuff, and tends to be very hot (to stop it recombining into boring old gas) and in order to be an effective weapon it also has to be very dense. The technical name for a blob of very hot, very dense matter released into the wild is an explosion.
The trick, therefore, is to throw your explosion hard enough and fast enough that it hasn't expanded and cooled into harmlessness by the time it reaches your enemy. The example I usually give is perhaps a little outdated these days, but should give you some idea of what a proper plasma gun might look like: MARAUDER. The intent there was to flick a plasmoid at a few percent of the speed of light, so that it might travel a few thousand kilometres and hit a target.
No confinement is really practical.
A related weapon is perhaps the (in)famous Casaba Howitzer, a nuclear shaped charge that grew out of the Project Orion nuclear pulse propulsion project. With a bit of clever engineering, you can get quite a lot of the oomph of a nuclear explosion to go in one direction, more or less. Quite a lot still goes out in other directions, so it is quite a hazardous thing to be anywhere near when it goes off. The effect will be to generate a cone of very fast, reasonably dense plasma that might be able to seriously damage targets quite some distance away.
And would a weapon like this be capable of earth-to-orbit defense?
The problem with operating a weapon that fires out matter in an atmosphere is that it has to bash through a minimum of ten tonnes of atmosphere in order to reach space. All of your potential targets now effectively have a huge amount of armour which you have to punch through, requiring an enormous amount of overkill at ground level in order to provide useful amounts of damage at the top of the atmosphere.
That's very wasteful, and potentially makes even firing the weapon quite dangerous for the landscape around it.
The Casaba Howitzer mentioned above would seem unlikely to help here, given how small a nuclear fireball usually is in comparison to the thickness of the atmosphere, and the obvious issues of detonating very large actual nuclear weapons inside the biosphere in which you live.
And if you could think of any way of creating high energy plasma from a reaction that could be contained in something like an artillery shell that would be great.
I have good news for you! The real world has had nuclear artillery since the 50s.
This removes most of the problems with trying to pin down plasma, especially very very hot, very dense plasma that will be radiating away a lot of energy because you're dealing with readily storable precursor materials which are compact, stable and have a decent shelf life.
This technique also readily lends itself to being fitting to rockets and fired into space, ensuring that all the plasma is delivered to where it needs to go instead of wasting most of it on the way up.