In the run up to the crisis, Maximilian had been seen as a potential candidate for the Imperial throne, but with typical caution, he had not made any announcement, and when the election came in 1619, he did not stand, allowing Ferdinand to become Emperor. He had been working for a peaceful solution of the Bohemian rebellion, and when Frederick, elector Palatine, agreed to become king of Bohemia, Maximilian was furious. He could accept neither a Protestant king of Bohemia, or a Spanish army in Germany to solve the problem, and was thus forced to take the field himself. On 8 October 1619 he signed an agreement with Ferdinand which gave him overall control of the war in Bohemia, the right to occupy any areas he captured, and in a secret clause, Ferdinand agreed to give him the Palatinate after the defeat of Frederick, something that would eventually become a millstone around his neck.
On 23 July 1620, Maximilian crossed the Austrian border at the head of the 25,000 strong army of the Catholic league, commanded by his general, Count Tilly, who as commander of the Bavarian army was to play a major part on the Imperial side. On 4 August, they forced the submission of the rebellious upper Austrians at Linz, before crossing into Bohemia on 26 September. The two armies soon came into contact, and the rebels started to retreat back towards Prague. Within sight of the city, the two armies met, and at Maximilian's insistence, the Imperial army attacked, winning the battle of the White Hill (8 November 1620). News of the defeat sent Frederick fleeing, and Maximilian was in Prague by the evening of the battle. By this point he was owed 3 million gulden by the Emperor, who gave him possession of Upper Austria in payment. The secret part of his agreement with the Emperor was implemented in 1623. On 23 February, Frederick was deposed as Elector Palatine, and on 25 February, Maximilian was created Elector Palatine for life, with the possibility that Frederick's children could be restored after Maximilian's death. This move was deeply unpopular with the other Electors, who largely refused to acknowledge him. This was a blatant breaking of the Imperial constitution, and destroyed Maximilian's position as defend of Germany liberties. It also linked Maximilian closely to the Imperial cause, and was a serious block to peace on many occasions.
At the time it looked as if Ferdinand was subservient to Maximilian, who had the only armed force in Germany, and was owed 16-18 million florins by the Emperor, who was forced to grant him temporary possession of Upper Austria and the Upper Palatinate, with the aim of regaining Austria through the permanent grant of the Palatinate. However, the taking of the Palatinate eventually forced Maximilian into a subservient position. This was not immediately apparent. During 1624, the Protestant nations of northern Europe, along with France, were arming to aid Frederick, and Maximilian was able to get the Catholic League to agree to strengthen Tilly's army, while in July he was reconciled with John George, elector of Saxony after the full details of Frederick's plotting was revealed. In 1625 he still saw safety in arms, and gave tacit support to Ferdinand when he raised an army under Wallenstein, while he was himself confirmed in overall control of the war. In May 1626 his rule of Upper Austria provoked a peasants revolt, which was only put down when Maximilian sent Pappenheim, a gifted cavalry general, with some of the elite Bavarian troops in November. By 1627 he was started to distrust Wallenstein, and with the Catholic League threatened to withdraw his support from Ferdinand unless Wallenstein was curtailed. Wallenstein had consistently undermined Tilly's army, forcing him into worse quarters, and stealing large numbers of his troops.
On 11 March 1628 Ferdinand made Wallenstein Duke of Mecklenberg, provoking the Princes. Maximilian was the leader of a Princely party that wanted to restore Germany to it's condition just after the battle of Lutter (27 August 1626), which would mean he kept his gains in the Palatinate while Wallenstein would lose Mecklenberg, while the elector of Saxony wanted to go one step further and restore Frederick to the Palatinate. What the electors were united on was their opposition to Wallenstein, and on 28 March they told Ferdinand that they would not elect his son Ferdinand as King of the Romans (effectively heir to the Empire), unless Wallenstein went. For the moment Ferdinand was strong enough to resist these calls, and on 6 March 1629 he issued the Edict of Restitution, which ordered a return of all church property lost since 1555. Maximilian had originally been in favour of this, but by the time of the Edict, his own gains were threatened by it. Indeed, if the Edict had been put in place across Germany the disruption could have been worse than that of the war itself. By 1630, Maximilian was openly admitting that he wanted the Emperor to disarm, and was willing to take French help to resist Wallenstein and the Spanish, who he still feared more than any other external threat. In August Wallenstein resigned, and Maximilian split from the French, expecting to regain his former control over Ferdinand, who did indeed restore him to overall command of the war, although only after failing to get his son appointed. Maximilian's shifting diplomacy now caused great problems for his commander, Tilly. In January 1631 France and Sweden signed a treaty that included a clause where they agree not to attack Maximilian's lands. On 8 May, Maximilian signed a secret treaty with France, agreeing not to assist their enemies.
This left Tilly in an impossible position. As commander of the Imperial army he was under a duty to attack Gustavus Adolphus's Swedes, but as commander of the Bavarian army he was forbidden from attacking either the Swedes or the elector of Saxony. This eventually forced Tilly to battle and defeat at Breitenfeld (17 September 1631), undoing all of Maximilian's gains of 1630. He started 1632 by proclaiming himself neutral, but did not act on his proclamation, and as Gustavus Adolphus advanced towards him, he rejoined Tilly's army (1 April 1632), and renounced his neutrality, inadvertently giving Gustavus the reason he wanted to invade Bavaria. Maximilian was present at the battle of the Lech (15 April 1632), and after both Tilly and his second in command were wounded, Maximilian saved his army through a quick retreat, although he lost his guns and baggage, while Tilly died of his wounds soon after. On 3 May, Gustavus started towards Bavaria. Maximilian was forced to chose between the Imperial cause or the good of Bavarian, and for once he chose the Imperial good, and abandoned Munich in order to defend Regensburg and his lines of communication with the Imperial armies. After the repeated defeated, Ferdinand was forced to recall Wallenstein, and on 11 July Maximilian and Wallenstein joined forces. Their alliance was initially successful, and they held the Swedes at bay until September, when Gustavus marched towards Austria.
Maximilian urged attack, but Wallenstein refused, and Maximilian retired with the remains of his army to defend Bavaria, where he spent the winter and spring of 1633. In March 1633, he was faced by a large Swedish army under Marshal Horn and another equally large German force under Bernard of Saxe-Weimar, and did not have the forces to resist them, but was saved by distrust between the two allies, and by a lack of supply in the enemy armies. Now Maximilian started to lose his grip of Bavaria, and by December between 20 and 30,000 peasants were in revolt, complaining about the quartering of troops in devastated areas. Maximilian was forced to pacify the rebels by agreeing to limit the areas his troops were quartered in. 1634 saw Maximilian at a low point. He had been forced out of his own Bavaria, forcing him to welcome the appointment of Ferdinand's son Ferdinand of Hungary as commander of the Imperial troops, and in 1635 to sign the Peace of Prague, forcing him to dissolve the Catholic league, and place his armies under direct Imperial command. For the first time, Maximilian found himself as a junior ally of the Emperor, commanding troops over whom he no longer had any support. Once again his seizure of the Palatinate had forced his hand - the only alternative to wholehearted support of Ferdinand was to join with Richelieu and France, but he was insisting on the restoration of the deposed children of Frederick in the Palatinate. In compensation for his newly junior position, Maximilian was confirmed in the Palatinate, and given one of Ferdinand's daughters in marriage, his first wife having died childless.
Despite his apparent weakness, Maximilian was soon showing signs of recovery. In 1636 the weakness of France led him to suggest an attack on Paris, which would knock France out of the war. Ferdinand eventually agreed, and Maximilian sent Johann von Werth, his cavalry general, and the best of his cavalry to take part in the attack, which by August had reached Amiens. However, another attack further south failed, and in November Maximilian was forced to recall Werth and his cavalry to defend Bavaria, forcing the invasion to be abandoned. Late 1636 saw a son born by his new wife, seen by some as reward for his return to Imperial loyalty. He also now agreed to the election of Ferdinand of Hungary as King of the Romans, just in time, as on 15 February 1637 Ferdinand II died, to be replaced by his son Ferdinand, now Emperor Ferdinand III. Maximilian spend the next few years recovering his strength and rebuilding his army. By 1643 he was once again dominant in the Imperial army, which now relied on Franz von Mercy, the Bavarian command, and Werth, his cavalry general. By 1644, Mercy and a comparatively small Bavarian army was able to hold off the combined French armies of Turenee and Enghien during the three battles of Freiburg, and although he was forced to withdraw, he was able extract most of his army. This improved Bavarian position forced Ferdinand III to take Maximilian's threat to make a separate peace seriously enough to force in to the negotiating table, and negotiations at the Congress of Westphalia began in December 1644.
At the peace conference Maximilian showed that he still feared Spanish intervention more than Swedish or French and he was determined to remove any reason for them to intervene in Germany by giving them most of what they wanted - Alsace for France and Pomerania for Sweden. Bavaria suffered again over the winter of 1646-47. A Swedish invasion overwhelmed Werth, and Maximilian refused to arm his people in fear of a peasants revolt, instead choosing a scorched earth policy, destroying mills and storehouses, and causing a famine the next year. A truce was only agreed in March, and put in place in April 1647. Under pressure, over the Winter of 1646 Maximilian finally agreed a compromise over the Palatinate. Charles Lewis, the son of Frederick, was given Heidelberg and the Rhinish Palatinate only, and restored to electoral college as the lowest ranked Elector, while Maximilian kept most of the Palatinate. In 1647 Maximilian made another attempt at neutrality, pointing out his age (74) and his young family as his reasons. This time he was forced back into the Imperial camp by the desertion of his cavalry general Werth, who in July attempted to take the Bavarian army with him. Even thought the army returned to Maximilian, he was himself frightened out of neutrality and back into the Imperial camp. Once again Bavarian suffered, and in the summer of 1648 the French rampaged across Bavaria in revenge for Maximilian's change of side, finally forcing Ferdinand III to sign the Peace of Westphalia. Although Maximilian had maintained most of his early gains, the war had left him an old man, with a devastated land and ruined army. His frequent changes of policy, and failure to maintain any middle party of German princes had helped condemn Germany to being the battleground for a conflict that by the time it ended had very little to do with Germany itself.