The Magen Avraham states that it is forbidden to count Jews, even if you didn’t mean to count, but were casting lots, and even for a matter concerning a mitzvah. It became common practice to count Jews by the words of a verse from the Torah, e.g., “Hoshiah et Amecha …” and other verses from the Torah which provide protection against the evil eye.