The spectra measurements of charged hadrons produced in the shower of a parton originating in the same hard scattering with a leptonically decaying Z boson are reported in lead-lead nuclei (Pb-Pb) and proton-proton (pp) collisions at a nucleon-nucleon center-of-mass energy of 5.02 TeV. Both Pb-Pb and pp data sets are recorded by the CMS experiment at the LHC and correspond to an integrated luminosity of 1.7 nb-1 and 320 pb-1, respectively. Hadronic collision data with one reconstructed Z boson candidate with the transverse momentum pT>30 GeV/c are analyzed. The Z boson constrains the initial energy and direction of the associated parton. In heavy ion events, azimuthal angular distributions of charged hadrons with respect to the direction of a Z boson are sensitive to modifications of the in-medium parton shower and medium response. compared to reference data from pp interactions, the results for central Pb-Pb collisions indicate a modification of the angular correlations. The measurements of the fragmentation functions and pT spectra of charged particles in Z boson events, which are sensitive to medium modifications of the parton shower longitudinal structure, are also reported. Significant modifications in central Pb-Pb events compared to the pp reference data are also found for these observables.