Microorganisms live in environments that inevitably fluctuate between mild and harsh conditions. As harsh conditions may cause extinctions, the rate at which fluctuations occur can shape microbial communities and their diversity, but we still lack an intuition on how. Here, we build a mathematical model describing two microbial species living in an environment where substrate supplies randomly switch between abundant and scarce. We then vary the rate of switching as well as different properties of the interacting species, and measure the probability of the weaker species driving the stronger one extinct. We find that this probability increases with the strength of demographic noise under harsh conditions and peaks at either low, high, or intermediate switching rates depending on both species ability to withstand the harsh environment. This complex relationship shows why finding patterns between environmental fluctuations and diversity has historically been difficult. In parameter ranges where the fittest species was most likely to be excluded, however, the beta diversity in larger communities also peaked. In sum, how environmental fluctuations affect interactions between a few species pairs predicts their effect on the beta diversity of the whole community.