Bear markets are never fun. But savvy investors know that economic downturns, while painful, can give you a chance to build enormous wealth in the stock market by buying shares of excellent companies at steeply discounted prices.

One such excellent company is Microsoft (MSFT -0.15%). Let's take a closer look at the company (and the stock) to see if this technology leader presents such an opportunity at the moment.

The bull case for Microsoft's stock

Powerful global trends drive Microsoft's expansion. Businesses are digitizing their operations and shifting them to the cloud, boosting demand for the tech titan's productivity software and cloud computing services. At the same time, billions of people around the world are turning to digital forms of entertainment, fueling the growth of Microsoft's popular Xbox gaming console and streaming offerings.

Microsoft's leadership positioned the company -- and its investors -- to profit handsomely from these trends. Since becoming CEO in 2014, Satya Nadella prioritized mobile- and cloud-based services, while de-emphasizing traditional desktop software licenses. That decision proved prescient. Microsoft now stands as a key enabler of the digital transformation megatrend. And its long-term investors are richer for it, despite the recent downturn in its stock price.

Microsoft's strong returns to shareholders over the past half-decade were driven by its sterling financial results. The tech giant continues to generate an astounding amount of profit and cash flow, including $73 billion in net income and $65 billion in free cash flow over the trailing 12 months. This impressive cash production allowed Microsoft to reward its investors with bountiful share repurchases and a steadily rising dividend income stream.

MSFT Stock Buybacks (Quarterly) Chart

MSFT Stock Buybacks (Quarterly) data by YCharts.

Microsoft's incredible cash flow generation and massive cash reserves -- which exceeded $100 billion as of June 30 -- also enable it to acquire other attractive growth companies. For example, Microsoft struck a deal to purchase video game leader Activision Blizzard for a whopping $68.7 billion in January to further bolster its gaming business.

Risks for investors to consider

Fears of a potential recession drive many businesses to pare back their technology investments. Evidence of this can be seen in the deceleration in Microsoft's year-over-year revenue growth to 12% in its most recent quarter from 18% in the prior quarter and 21% in the year-ago period. Yet the tech juggernaut's diversified business lines and fortress-like balance sheet should allow it to invest in its own operations straight through the current economic downturn. This, in turn, ought to further strengthen Microsoft's competitive position versus its smaller and less financially sound rivals.

A select few competitors, however, can match Microsoft's scale. Amazon (AMZN 0.90%) for one, is actually the market leader in cloud computing services. Amazon Web Services (AWS) holds a formidable presence in the cloud infrastructure arena, with a 34% market share, according to Synergy Research Group. Microsoft's Azure platform is second with a 21% share. This gap could narrow in the coming years, as Azure is growing at a faster pace than AWS. Still, this is a battle that investors should watch closely.

Apple (AAPL 0.42%) and Alphabet (GOOG 1.15%) (GOOGL 1.01%) should also not be overlooked. Apple's Macs and Google's Chromebooks are gaining ground in the personal computer marketplace. Macs hold a large share of the high-end computer market, while Chromebooks are popular among cost-sensitive buyers like schools and students. However, Microsoft's Windows operating system still leads in the enormous corporate desktop PC market, with a commanding 76% share.

Microsoft stock has a reasonable valuation

Like many tech stocks, Microsoft's share price is down sharply in 2022. After a nearly 30% decline, its shares can now be had for roughly 23 times Wall Street's earnings projections for this year and less than 20 times analysts' estimates for next year. That's a sensible price to pay for a technological and financial goliath that's forecast to grow profits by more than 15% annually over the next half-decade.

Income-focused investors will also appreciate Microsoft's modest but rapidly growing dividend. Just days ago, its board of directors approved a 10% increase to its quarterly cash payout, bringing its current yield to a respectable (for a tech growth stock) 1.2%.

So, is Microsoft's stock a buy?

With its strong position within the cloud, gaming, and digital transformation megatrends, Microsoft has many years of growth still ahead. Although powerful rivals continue to pose challenges, the tech giant's entrenched position and financial fortitude should allow it to hold its competitors at bay in some markets and expand its presence in others.

Better still, long-term investors currently have the opportunity to begin or add to a position in this proven winner at a significantly discounted price. For all these reasons, Microsoft's stock looks like an attractive buy today.