  Planetary nebulae are thought to be formed when a slow wind from the
progenitor giant star is overtaken by a subsequent fast wind generated as the
star enters its white dwarf stage$^{1}$. A shock forms near the boundary
between the winds, which creates a relatively dense shell that provides the
characteristic appearance of a planetary nebula. A spherically symmetric wind
will produce a spherically symmetric shell, yet over half of known planetary
nebulae are not spherical; rather, they are elliptical or bipolar in
shape$^{2}$. While a magnetic field could launch and collimate a bipolar
outflow, the origin of such a field has hitherto been unclear, as previous work
suggested that a field could not be generated${^3}$. Here we show that an
asymptotic-giant-branch (AGB) star can indeed generate a strong magnetic field,
in a dynamo at the interface between a rapidly rotating core and the more
slowly rotating envelope of the star. The field is strong enough to shape the
bipolar outflows that produce the observed bipolar planetary nebulae. Magnetic
braking of the stellar core during this process may also explain the
puzzlingly$^{4}$ slow rotation of most white dwarf stars.
