In this study, we have spectroscopically investigated the plasma generated by a Q-switched Nd:YAG laser operating at its fundamental wavelength of 1064 nm focused on magnesium (Mg) and titanium (Ti) target samples in the air under atmospheric pressure. We employed circular cavities of radii (2.5, 3.0, and 3.5 mm) and a square cavity to investigate the cavity confinement effect on the spectral emission intensities of the plasmas. We observed that the circular cavity of radius 2.5 mm had the maximum signal enhancement, and this can be attributed to the compression of the plasma and reheating by the reflected shock waves. The maximum enhancement factor of the Mg I-518.4 nm line was reached at approximately 3.8, 3.4, and 2.8 with a circular cavity of radius 2.5, 3.0, and 3.5 mm, respectively, at a delay time of 350 ns and a laser energy of 350 mJ. By applying varying external magnetic fields (0.47, 0.62, 0.91, and 1.23 T) across the generated plasma, the plasma parameters such as electron temperature and number density have been investigated. From our results, we observed that the radius of the cavity had a tremendous effect on the enhancement of the emission signal intensities. We also found that the increase in the electron temperature and the number density can be attributed to the increase in the applied magnetic field and the laser energy. From our calculations, the value of β, which was less than 1 for all the cases, confirms that there was a plasma confinement at the presence of the magnetic field.