5 Answers
5

I know of one posek who proved that this is perfectly fine, from the fact that many seforim cite traditional segulos to determine a fetus' gender. Obviously, then, there is nothing improper about knowing the gender beforehand. (One could argue that knowing with certainty is worse, but this seems like a distinction without a difference. And besides, even sonography is far from foolproof.)

"one posek who proved" is pretty vague. Can you provide the source, please, assuming that this was published?
– הנער הזהMar 23 '14 at 4:12

@Matt - As far as I know, the reasoning isn't published. I heard it by word of mouth from a source I deemed reliable, but I don't feel right about mentioning the name of the posek without his approval. In my opinion, the reasoning is solid enough to stand on its own.
– DaveMar 26 '14 at 18:52

The Shailos Rav (by Rav Chaim Kanievsky), in 12:2, says one should not do it based off the Koheles Rabbah 11:5 and also can be seen in Medrash Rabbah Toldos 65:12 that says seven things are hidden from man and one of them is knowing the gender of a child in the womb .

It seems that it it Muttar, though some would say it isn't ideal.
R' Avraham S. Avraham writes the following (Nishmas Avraham Vol 2, p109):

.. Incidentally, is a woman permitted to undergo sonography (ultrasound) to learn what the sex of the fetus is? The Midrash (Midrash Rabah, Bereishis 65:12) enumerates seven things whichare hidden from man, one of them being the sex of the fetus.

The Yefei To'ar gives two reasons for this: This knowledge may grieve the parents throughout the pregnancy if the sex of the baby is not of their choice; if they wish for a boy and discover that this is indeed the sex of the fetus, it will diminish their joy at the birth and the mother will not have the full reward for her labor pains.

On the other hand, the Gemara (Pesachim 54b) also enumerates the seven things that are hidden from man but the sex of the fetus is not one of them.

Rav Neuwirth shlita [zatzal] told me that it woould be preferable not to undergo sonography for this purpose.

It sounds like superstition. However, I know some don't want to know the baby's sex, so that they have a level of enjoyment after the long birthing process of finding out whether the baby is a boy or a girl. I am sure others might have other reasons. I don't know of any halachik basis.

Why is it enjoyable to find out after? You know your getting one of two options, and you'll be happy either way.
– Double AA♦Jun 23 '17 at 2:52

@DoubleAA I can't speak personally on this issue. My wife shared this thought with me. Perhaps it is the discovery of what it is, adds an element of excitement. It is true it is one of two options, but finding out which option it truly is can be enjoyable to the parents.
– RCWJun 26 '17 at 6:00