The Senate yesterday voted to ban the sale of Toshiba products in the United States for at least two years because the company sold sophisticated machinery to the Soviet Union that allowed Moscow to make its submarines quiet enough to avoid detection.

Similar legislation is pending in the House, where two committees are holding hearings, and its passage there is virtually assured, Hill sources said. Administration officials indicated they oppose the measures.

Toshiba, whose U.S. sales last year accounted for about $4 billion of its global sales of $22.65 billion, is a major supplier of consumer electronics products to the United States. It makes television sets, tape recorders and refrigerators, as well as computers and electronics products for industry.

The Senate action, an amendment to the massive trade bill, came on a 92 to 5 vote that reflected growing congressional anger over the sale by Toshiba and a government-owned Norwegian company, Kongsberg Vaapenfabrik, of eight high-technology machine tools to the Soviets.

The equipment allowed the Soviets to retool their submarine propellers so they run quietly, erasing the U.S. advantage in detecting and tracking their undersea fleet. The machine tools also are used on new Soviet aircraft carriers, making them far more maneuverable, U.S. defense and intelligence sources said.

The measure passed by the Senate would ban Toshiba and Kongsberg sales for between two and five years, at the president's discretion, and would impose similar bans on the sale of products made by companies that violate similar export-control rules in the future. It covers parent companies and all subsidiaries.

The penalty would amount to the stiffest sanction ever imposed for violations of export-control rules set by the multilateral body that polices high-technology trade with the Soviet bloc. That body, known as Cocom, is composed of the NATO allies except Iceland.

The Senate approved on a voice vote an amendment by Sen. Jesse Helms (R-N.C.) that would demand compensation for the cost to U.S. taxpayers of countering the upgraded Soviet military as a result of export-control violations. In this case, intelligence estimates put the U.S. costs as high as $30 billion.

"What Toshiba and Kongsberg did was ransom the security of the United States for $17 billion" in sales to the Soviet Union, said Sen. John Heinz (R-Pa.). "The market price of betrayal has gone up from 30 pieces of silver."

Heinz cosponsored the amendment along Sen. William Proxmire (D-Wis.), chairman of the Senate Banking Committee that has jurisdiction over export-control legislation in the Senate, and Sen. Jake Garn (R-Utah), ranking minority member of the committee.

The parent company, Toshiba Corp., has tried to distance itself from the sale. It has insisted that the transaction was made by an independent subsidiary, Toshiba Machines Inc., of which it owns 51 percent.

The legislation approved by the Senate would allow for the continued sale of spare parts to existing equipment and components needed by U.S. manufacturers for their products.

Another waiver would allow Kongsberg to continue selling its Penguin anti-ship missile to the U.S. Navy. That is the major product that the near-bankrupt Swedish firm sells to the United States.

Norwegian Defense Minister Johan Holst was in Washington last week trying to mollify the Reagan administration and soothe Congress.

Staff aides said they wanted the legislation to be tight enough to prevent an easy waiver by the president. Administration officials said it appeared that the plan was too tight to win White House support. In addition, it appeared that the administration was trying to use the congressional furor to gain greater support from Japan and Norway at next month's Cocom meeting in Paris.

The Senate beat back an even tougher amendment by Sen. Richard C. Shelby (D-Ala.) that would have put a permanent ban on Toshiba and Kongsberg sales in the United States. The vote was 78 to 19 against Shelby's amendment.

Shelby, along with Sens. Daniel J. Evans (R-Wash.), Wendell H. Ford (D-Ky.), Mark Hatfield (R-Ore.) and Donald W. Riegle Jr. (D-Mich.), voted against the Proxmire-Garn-Heinz measure.

Garn opposed Shelby's measure, saying it went too far and could end up hurting U.S. consumers and the national security. In addition, he said, with no chance to get out from under a ban, other countries and their companies would be less likely to cooperate in investigations of export-control violations.

In statements at a House Foreign Affairs subcommittee yesterday, lawmakers attacked Toshiba and Kongsberg for endangering U.S. security.

Subcommittee Chairman Don Bonker (D-Wash.) called the sale an "egregious violation of Western security and our ability to detect Soviet submarines. Clearly damage has been done to U.S. and allied security interests."

Rep. Duncan Hunter (R.-Calif.) called it "an act of treason and treachery."