It is permissible to have medical tests to find out the sex of the foetus, whether it is male or female. This is one of the ways and means that Allah has made available to people to check on the condition of the foetus after it has been formed. There are still many unseen matters having to do with the foetus that no one knows except Allah, such as its condition before it is formed, and its provision, lifespan, and whether it is blessed (destined for Paradise) or doomed (destined for Hell).
See also the answer to question no. 12368.
And Allah knows best.
Reproduced from Islam QA