The eminent Muslim scholar and renowned Da`iyah, Sheikh `Abdel Khaliq Hasan Ash-Shareef, states: “The Sunnah Prayer that is observed to greet the mosque is called salat tahiyyat Al-masjid (greeting the mosque Prayer). It is sunnah to perform it upon entering the mosque at any time except theDetested Times for Performing Salah. However, the Shafi`i scholars have permitted perform tahiyyat Al-Masjid Prayer in the reprehensible time of offering Prayers.
Also, there will be no need for this Prayer if upon entering the mosque one performed any other Prayer either obligatory or non-obligatory Prayer. That is because the aim behind it is to show respect to the mosque upon entering it.