Cohesive powders tend to form porous aggregates which can be compacted by applying an external pressure. This process is modelled using the Contact Dynamics method supplemented with a cohesion law and rolling friction. Starting with ballistic deposits of varying density, we investigate how the porosity of the compacted sample depends on the cohesion strength and the friction coefficients. This allows to explain different pore stabilization mechanisms. The final porosity depends on the cohesion force scaled by the external pressure and on the lateral distance between branches of the ballistic deposit. Even if cohesion is switched off, pores can be stabilized by Coulomb friction alone. This effect is weak for round particles, as long as the friction coefficient is smaller than 1. However, for non-spherical particles the effect is much stronger.