On 7 July 1838 Stansbury was granted a commission as a first lieutenant in the newly formed Army Corps of Topographical Engineers and was advanced to captain in 1840. From 1838 to 1849 he directed projects for the corps in the Great Lakes region, at Portsmouth, New Hampshire, and, during the Mexican War, at some fortifications in the Dry Tortugas in the Gulf of Mexico.
The supreme assignment of his army career was to lead an expedition in 1849 to the valley of the Great Salt Lake. His orders directed him to survey and map the Great Salt Lake and its valley as well as Utah Valley; to evaluate the various emigrant roads in the area, including the Oregon Trail; and to examine and report on the capability of the Mormon community at Salt Lake City to provide food and supplies for overland travelers. During the year he and his second in command, Lieutenant J.W. Gunnison, spent in Utah, Stansbury completed his assignment and produced a remarkable Report, which also went through several editions as a private publication. Stansbury's Report along with Gunnison's book, The Mormons, provided the outside world with an objective look at the Mormons of Utah as well as with a scientific appraisal of the resources and fauna and flora of this section of the Great Basin.
Captain Stansbury spent the next years, until the outbreak of the Civil War, improving harbors in the Great Lakes and building roads in Minnesota Territory. When the war came, he was appointed as mustering officer at Columbus, Ohio, and later was placed in charge of recruiting for the state of Wisconsin. Stansbury served only forty-five days in this post before he died on 13 April 1863 at the age of fifty-six of "disease of the heart." His obituary noted that his early death came as a result of the "over-exertions and hardships" endured during his Great Salt Lake expedition. Stansbury was buried at St. Paul, Minnesota, in 1863.