Despite all the things that went wrong for Microsoft (MSFT 2.54%) in 2013 -- and there were many -- its shareholders have enjoyed a tremendous rally. Baring a major sell-off in the final weeks of the year, Microsoft shares should close up about 40%, a particularly impressive gain for such a large component of the Dow Jones Industrial Average (^DJI 0.19%).

In fact, Microsoft was one of the best large-cap stocks you could've owned in 2013. In addition to a steady stream of dividend payments, Microsoft has greatly outperformed rival Apple (AAPL 0.37%) and nearly kept pace with Google (GOOGL 9.62%):

MSFT Chart

MSFT data by YCharts.

The PC in decline
Sales of traditional personal computers hit a wall in 2013, with the year looking to go down as the worst in PC history. In the first quarter, PC shipments fell 13.9% for the largest drop on record. Declines in subsequent quarters were not as steep, but were unquestionably contractionary: In the second quarter, IDC reported an 11% drop, and in the third quarter, a 8.6% decline.

As Microsoft's Windows business depends on the sale of traditional PCs, a shrinking market is obviously not great for the software giant. Microsoft admitted that, at least among consumers, demand for traditional PCs appears to be declining at a rapid pace. On Microsoft's earnings call back in July, the company said consumer demand fell 20%, though business demand was stable.

Surface sales disappoint
Consumers seem to be flocking to mobile devices, most of which are sold by Apple or powered by Google's mobile operating system, Android. Microsoft's attempt at breaking into this market with its own Surface tablet was largely a failure in 2013, with the company taking a $900 million writedown on unsold Surface inventory. 

It wasn't for lack of advertising --  Microsoft reportedly spent more than $1 billion promoting Windows 8 and the Surface tablet. Many of those ads pitted Microsoft's own tablet (or tablets made by its hardware partners) against Apple's iPad. While Microsoft's ad campaign pointed out that the Surface came with Office and was better suited to desktop work, total Surface sales fell far short of the iPad. Microsoft has yet to release exact sales figures, but in July estimates put the figure at under 2 million. In comparison, Apple sold 14.1 million iPads last quarter.

Ballmer announces exit after reorganization
Despite the apparent failure of its new Windows 8 strategy, the company essentially doubled down on it July when CEO Steve Ballmer announced a reorganization intended to focus Microsoft around "devices and services." Microsoft emphasized its commitment to the strategy by purchasing Nokia's handset business in September.

Interestingly, Ballmer won't be around to see the shift through. In August, Ballmer said he would retire within a year, leaving the door open for the next CEO to complete the company's transformation. That will likely be the major catalyst for Microsoft shares in 2014.

2013 in context
Overall, 2013 was the first year of Microsoft's transformation. With Google and Apple encroaching on Microsoft's turf, the company has decided to fight back by realigning its business to a more cohesive unit. No doubt, it didn't go as well as it could've -- the ongoing decline in the PC market, coupled with Microsoft's inability to break into mobile, seemed to hang over the company, and could've been partially responsible for Ballmer's resignation.

Yet investors didn't seem to mind, perhaps feeling confident that the transformation will work out in the long run, or that new initiatives focused around the cloud will ultimately offset any declines in Microsoft's existing products. Either way, it was a great year to be a Microsoft shareholder.