It's largely dependent on the undead in question, and at times on the DM.
As you mentioned a necromancer being involved, I will first address undead minions available to playable necromancer wizards through spells: ghasts, ghouls, mummies, skeletons, wights, and zombies. Those are also the common undead minions of undead NPCs. I will refer to the entries presented in the Monster Manual, as monster description are as important as their stat blocks. Emphasis within this answer is mine.
As an extra note: generally, in D&D, most of the things that give a creature its personality and memories is just their soul. And most undead are not created using their soul: they are cadavers animated by magic of some sort. Some aspects of the soul can still linger in some undead, whilst some others might have a (trapped?) soul within their body.
Ghouls and Ghasts: up to the DM.
The entries for the ghoul do not refer to it directly, but it is inferred they keep memories, or can keep them, through their "origin story". We gather from this that at the very least the original ghoul Doresan kept its memories of elven deities after having served Orcus, the responsible for his transformation. Though this gives not clear cut answer for every ghoul.
However, a ghast is described as being more cunning than a ghoul, depicted as a "little more than a savage beast". We can imply that, even if ghouls can keep memories, the ghast is the version of ghouls that ultimately could reminisce more clearly or act on those.
Mummies: Yes, but...
An undead mummy is created when the priest of a death god or other dark deity ritually imbues a prepared corpse with necromantic magic. [...] As a mummy endures in undeath, it animates in response to conditions specified by the ritual. [...] Once deceased, an individual has no say in whether or not its body is made into a mummy.
In essence, mummies became slaves through the rite performed to make them undead. The second to last paragraph, however, tells us they do, in fact, keep their memories.
Though they seldom bother to do so, mummies can speak. As a result, some serve as undead repositories of lost lore, and can be consulted by the descendants of those who created them. Powerful individuals sometimes intentionally sequester mummies away for occasional consultation.
Skeletons: No. But still...
We get some info about it throughout their description (please, do not use their Intelligence score as a basis):
[...] rise of their own accord in places saturated with death and loss, awakened by stirrings of necromantic energy or the presence of corrupting evil. [...] Whatever sinister force awakens a skeleton infuses its bones with a dark vitality. [...] This energy motivates a skeleton to move and think in a rudimentary fashion, though only as a pale imitation of the way it behaved in life. An animated skeleton retains no connection to its past, although resurrecting a skeleton restores it body and soul. They can't read, speak, emote, or communicate in any way except to nod, shake their heads, or point. Still, skeletons are able to accomplish a variety of relatively complex tasks. [...] Although they lack the intellect they possessed in life, skeletons aren't mindless.
The "but still..." part refers to this other section:
Independent skeletons temporarily or permanently free of a master's control sometimes pantomime actions from their past lives, their bones echoing the rote behaviors of their former living selves. [...] When skeletons encounter living creatures, the necromantic energy that drives them compels them to kill unless they are commanded by their masters to refrain from doing so.
I'd say an uncontrolled skeleton is presumed to start acting on former memories, but even with a hat of disguise, that undead cannot speak.
Wights possess the memories and drives of their formerly living selves. They will heed the call of whatever dark entity transformed them into undead, swearing oaths to appease their new lord while retaining their autonomy. Never tiring, a wight can pursue its goals relentlessly and without distraction.
Sadly, that's all we got for wights, but it seems enough to give a clear answer.
Zombies: No. Period.
Sinister necromantic magic infuses the remains of the dead [...] Most zombies are made from humanoid remains, [...] A zombie retains no vestiges of its former self, its mind devoid of thought and imagination. A zombie left without orders simply stands in place and rots unless something comes along that it can kill. The magic animating a zombie imbues it with evil, so left without purpose, it attacks any living creature it encounters.
That being said, those are just the common undead minions. Undead can be extremely varied and/or even independent. As an example, vampires are said to either keep or not keep memories from their former lives, and those memories they do not keep cling to them in the form of "cravings", such as surrounding themselves with art pieces. And even then, a DM can always rule otherwise.
What I'm writing here is the RAW interpretation of undead that in a generic campaign a playable necromancer might command. If you need more information about individual undead creatures, their description might give you an answer or a hint; if not, ask your DM (or come up with an interpretation, if you are the DM).