The artificial intelligence (AI) market is booming, but not all AI stocks are worth buying. Like every technological shift, there will be winners and losers. Investors can't go wrong with companies that are profitable and growing revenue, like Snowflake (SNOW 3.69%) and Microsoft (MSFT 1.82%).

The IDC forecasts spending on generative AI software will grow 73% annually through 2027. Here's why these two companies would make great buy-and-hold investments over the next 20 years.

1. Snowflake

Snowflake has been a rising star in the cloud computing market. Revenue increased from just $592 million to $2 billion over the last three years. Snowflake's data cloud platform allows companies to work with their data more cost-efficiently, and it's starting to see growing interest from customers who want to tap the opportunities AI technology brings to the enterprise.

One of the top hurdles companies are running into as they explore ways to use AI is access to quality data. AI models are only as good as the data used to train them. Snowflake solves this problem with its data marketplace, where customers can buy data from other companies. This is an increasingly popular feature, as Snowflake said 28% of its customers are sharing data on the marketplace, up from 22% over the year-ago quarter.

The most important reason Snowflake can make investors great returns is that it is already generating positive free cash flow from its business. Snowflake produced $630 million in free cash flow over the last four quarters, which shows a healthy margin compared to its $2 billion in trailing revenue. Snowflake has the cash to invest in innovative products that can widen its economic moat.

Snowflake's Document AI is a promising opportunity. It can answer a user's questions using data from any document. The time saved with this service could be game-changing since most of the world's data is stored in unstructured documents.

Moreover, the company is still in the early innings of scaling its business. This means investors can count on the company's expanding margins to boost earnings and free-cash-flow growth. Wall Street analysts expect Snowflake to grow earnings by 60% annually over the next few years.

Snowflake has a long runway of revenue growth, and it should grow profits even faster. This could lead to wealth-building returns for investors who can patiently hold shares for the next 20 years.

2. Microsoft

Microsoft's competitive advantage is based on the familiarity that millions of consumers and businesses have with software like Office and Windows.

The company's lucrative software and cloud businesses are key drivers of high-margin revenue growth. This translated to $63 billion in free cash flow over the last year, and Microsoft is investing its cash to completely transform itself into an AI-first services business that can drive more growth and keep the stock climbing in value for years to come.

Microsoft has said AI is the "foundation" for all the software it is creating, and its large base of customers means it is already positioned to potentially rake in billions in new revenue off subscriptions to new AI services.

The software giant recently expanded the availability of Copilot, a generative AI assistant. Microsoft will charge $20 per month for Copilot Pro for individuals, while business customers will pay $30.

Copilot is part of Microsoft's strategy to transform Windows into an AI-first platform, and it's already building some momentum. Over the last year, the number of devices running Windows 11 doubled. Microsoft is an established software brand well positioned to monetize AI software.

Analysts expect Microsoft to grow earnings by 14% per year. This rate of growth is consistent with Microsoft's previous 10-year record.

A sign of a great business is that it continues to adapt to shifts in technology. Microsoft has successfully navigated the shift from a PC-centric world to cloud computing, and it seems AI will make Microsoft only more relevant. The stock won't make you rich overnight, but it's a long-term growth machine that can likely outperform the broader stock market.