The prefrontal cortex (PFC) is essential for working memory (WM) and has primarily been viewed as being responsible for maintaining information over a delay, but it is unclear whether it also plays a more general role during WM. Using task phase-specific optogenetic silencing of pyramidal neurons in the medial PFC (mPFC) of mice performing a spatial WM task, we find that the mPFC is required not only during the delay phase of the task but also during other phases requiring the encoding and retrieval of spatial information. Imaging of mPFC pyramidal neurons reveals that they are most strongly influenced by the animals' position and running direction, indicating a fundamental role in spatial navigation. Pyramidal neuron ensembles also represent to-be-remembered goal locations in a dynamic manner. Taken together, these results delineate the functional contribution of mPFC pyramidal neurons to WM, extending their role beyond the maintenance of information.