This was homework problem in my undergrad chem engr You do an energy balance. The piston did work on the gas. Assume reversible compression. That work had to go somewhere. And you need to account for the potential energy of compressed gas. I forget he detail but the work done was not exactly the potential energy and the difference has to be accounted for as temperature. Not exactly the same throttling a gas as that is irreversible but if you then throttle the gas you get adiabatic cooling. If you then just release the piston and lets is oscillate it will do less work as it was irreversible and you will get some heating via random kinetic energy being formed. You can only take credit for work done against the external pressure.