New tax rules are hastening automation and modernizing in U.S. factories by giving manufacturers an incentive to buy machinery and boost productivity in a tight labor market.

For the next five years, the revised tax code allows companies to immediately deduct the entire cost of equipment purchases from their taxable income. Previously, companies generally were allowed to write off only a portion of the cost in a single year.

The change is encouraging manufacturers to install robots and replace aging machines sooner than planned.

“We probably would have put it off another year” without the tax incentives, said Ken Mathas, president of Cornell Forge Co., a Chicago maker of gears and other components for heavy machinery. Mr. Mathas said he plans to spend at least $1.5 million this year to add three or more robots to a production line in lieu of workers he is struggling to find.

U.S. manufacturers already are benefiting from a global economic upswing, a weaker dollar that has made American products more competitive overseas and improved business sentiment at home.