<Page> EXHIBIT 5.1 [Letterhead of Wilson Sonsini Goodrich & Rosati] February 25, 2002 Hewlett-Packard Company 3000 Hanover Street Palo Alto, California 94304 RE: HEWLETT-PACKARD COMPANY--REGISTRATION STATEMENT ON FORM S-3 Ladies and Gentlemen: At your request, we have examined the Registration Statement on Form S-3 (the "Registration Statement"), to be filed by Hewlett-Packard Company, a Delaware corporation (the "Company"), with the Securities and Exchange Commission on or about February 25, 2002. The Registration Statement relates to the proposed sale from time to time pursuant to Rule 415 of the Securities Act of 1933, as amended (the "Securities Act"), of the following securities of the Company with an initial aggregate offering price of up to $3,000,000,000 or the equivalent thereof: (i) senior debt securities (the "Senior Debt Securities"); (ii) subordinated debt securities (the "Subordinated Debt Securities and collectively with the Senior Debt Securities, the "Debt Securities"); (iii) shares of common stock, $0.01 par value per share (the "Common Stock"); (iv) shares of preferred stock, $0.01 par value per share (the "Preferred Stock"); (v) depositary shares (the "Depositary Shares") of the Company representing a fractional interest in a share of Preferred Stock; and/or (vi) warrants to purchase Debt Securities, Preferred Stock or Common Stock (the "Warrants") of the Company (the Debt Securities, Common Stock, Preferred Stock, Depositary Shares and Warrants are collectively referred to herein as the "Securities"). The Securities are to be sold from time to time as set forth in the Registration Statement, the Prospectus contained therein (the "Prospectus") and the supplements to the Prospectus (the "Prospectus Supplements"). The Senior Debt Securities are to be issued pursuant to a Senior Debt Securities Indenture entered into between the Company and J.P. Morgan Trust Company, National Association (formerly known as Chase Manhattan Bank and Trust Company, National Association) (the "Trustee") and dated as of June 1, 2000 (the "Senior Indenture"). The form of Senior Indenture is filed as an exhibit to the Company's registration statement on Form S-3 (file # 333-30786) and incorporated by reference into the Registration Statement. The Subordinated Debt Securities are to be issued pursuant to a Subordinated Debt Securities Indenture (the "Subordinated Indenture" and taken together with the Senior Indentures, the "Indentures") to be entered into between the Company and the Trustee, the form of which is filed as an exhibit to the Company's registration statement on Form S-3 (file # 333-30786) and incorporated by reference into the Registration Statement. Each of the Indentures may be supplemented in connection with the issuance any particular series of Debt Securities, by a supplemental indenture or other appropriate action of the Company creating such series of Debt Securities. The Securities are to be sold pursuant to an underwriting agreement, purchase agreement, agency agreement or similar agreement (collectively an "Underwriting Agreement"). We have examined instruments, documents and records that we deemed relevant and necessary for the basis of our opinion hereinafter expressed. In such examination, we have assumed (a) the authenticity of original documents and the genuineness of all signatures; (b) the conformity to the originals of all documents submitted to us as copies; (c) the truth, accuracy and completeness of the information, representations and warranties contained in the records, documents, instruments and certificates we have <Page> Hewlett-Packard Company February 25, 2002 Page 2 reviewed; (d) the Registration Statement, and any amendments thereto (including any post-effective amendments), will have become effective under the Securities Act; (e) a Prospectus Supplement will have been filed with the Securities and Exchange Commission describing the Securities offered thereby; (f) all Securities will be issued and sold in compliance with applicable federal and state securities laws and in the manner stated in the Registration Statement and the applicable Prospectus Supplement; (g) a definitive Underwriting Agreement with respect to any Securities offered will have been duly authorized and validly executed and delivered by the Company and the other parties thereto prior to the issuance of the Securities; (h) any securities issuable upon conversion, exchange, redemption or exercise of any Securities being offered will be duly authorized, created and, if appropriate, reserved for issuance upon such conversion, exchange, redemption or exercise and (i) with respect to shares of Common Stock or Preferred Stock offered, there will be sufficient shares of Common Stock or Preferred Stock authorized under the Company's certificate of incorporation, as amended, and not otherwise reserved for issuance. Based on such examination, we are of the opinion that: 1. With respect to Debt Securities to be issued under either the Senior Indenture or Subordinated Indenture, when (A) the Trustee is qualified to act as Trustee under the Senior Indenture or Subordinated Indenture, as applicable, (B) the Trustee has duly executed and delivered the Subordinated Indenture or Senior Indenture, as applicable, (C) the Senior Indenture or Subordinated Indenture, as applicable, has been duly authorized and validly executed and delivered by the Company to the Trustee; (D) the Senior Indenture or Subordinated Indenture, as applicable, has been duly qualified under the Trust Indenture Act of 1939, as amended, (E) the Board of Directors of the Company or a duly constituted and acting committee thereof (such Board of Directors or committee, including the Executive Committee of the Board of Directors, being hereinafter referred to as the "Board") has taken all necessary corporate action, and appropriate corporate officers of the Company have taken all appropriate action in accordance with such corporate action to approve the issuance and terms of such Debt Securities, the terms of the offering thereof and related matters, and (F) such Debt Securities have been duly executed, authenticated, issued and delivered in accordance with the provisions of the Senior Indenture or Subordinated Indenture, as applicable, and the applicable definitive Underwriting Agreement approved by the Board upon payment of the consideration therefor provided for therein, such Debt Securities will be validly issued and will constitute valid and binding obligations of the Company, enforceable against the Company in accordance with their terms; 2. With respect to shares of Preferred Stock, when both (A) the Board has taken all necessary corporate action to approve the issuance and terms of the shares of Preferred Stock, the terms of the offering thereof, and related matters, including the adoption of a Certificate of Amendment relating to such Preferred Stock (a "Certificate") and the filing of such Certificate with the Secretary of State of the State of Delaware, and (B) certificates representing the shares of Preferred Stock have been duly executed, countersigned, registered and delivered either (i) in accordance with the applicable definitive Underwriting Agreement approved by the Board upon payment of the consideration therefor (not less than the par value of the Preferred Stock) provided for therein or (ii) upon conversion or exercise of such Security pursuant to the terms and provisions of such Certificate for the consideration approved by the Board (not less than the par value of the Preferred Stock), then the shares of Preferred Stock will be validly issued, fully paid and nonassessable; 3. With respect to the Depositary Shares, when (A) the Board has taken all necessary corporate action to approve the issuance and terms of the Depositary Shares, the terms of the offering thereof, and related matters, including the adoption of a Certificate relating to the Preferred Stock underlying such Depositary Shares and the filing of the Certificate with the Secretary of State of the State of Delaware, (B) the Deposit Agreement (the "Deposit Agreement") or Agreements relating to the Depositary Shares <Page> Hewlett-Packard Company February 25, 2002 Page 3 and the related Depositary Receipts have been duly authorized and validly executed and delivered by the Company and the Depositary appointed by the Company, (C) the shares of Preferred Stock underlying such Depositary Shares have been deposited with a bank or trust company (which meets the requirements for the Depositary set forth in the Registration Statement) under the applicable Deposit Agreement, and (D) the Depositary Receipts representing the Depositary Shares have been duly executed, countersigned, registered and delivered in accordance with the appropriate Deposit Agreement and the applicable definitive Underwriting Agreement approved by the Board upon payment of the consideration therefor provided for therein, the Depositary Shares will be validly issued; 4. With respect to shares of Common Stock, when both (A) the Board has taken all necessary corporate action to approve the issuance of and the terms of the offering of the shares of Common Stock and related matters and (B) certificates representing the shares of Common Stock have been duly executed, countersigned, registered and delivered either (i) in accordance with the applicable definitive Underwriting Agreement approved by the Board upon payment of the consideration therefor (not less than the par value of the Common Stock) provided for therein or (ii) upon conversion or exercise of any other Security, in accordance with the terms of such Security or the instrument governing such Security providing for such conversion or exercise as approved by the Board, for the consideration approved by the Board (not less than the par value of the Common Stock), then the shares of Common Stock will be validly issued, fully paid and nonassessable; and 5. With respect to the Warrants, when (A) the Board has taken all necessary corporate action to approve the creation of and the issuance and terms of the Warrants, the terms of the offering thereof, and related matters, (B) the Warrant Agreement (the "Warrant Agreement") or Agreements relating to the Warrants have been duly authorized and validly executed and delivered by the Company and the Warrant Agent appointed by the Company, and (C) the Warrants or certificates representing the Warrants have been duly executed, countersigned, registered and delivered in accordance with the appropriate Warrant Agreement or Agreements and the applicable definitive Underwriting Agreement approved by the Board upon payment of the consideration therefor provided for therein, the Warrants will be validly issued and will constitute valid and binding obligations of the Company, enforceable against the Company in accordance with its terms. Our opinion that any document is legal, valid and binding is qualified as to: (a) limitations imposed by bankruptcy, insolvency, reorganization, arrangement, fraudulent conveyance, moratorium or other laws relating to or affecting the rights of creditors generally; (b) rights to indemnification and contribution, which may be limited by applicable law or equitable principles; and (c) general principles of equity, including without limitation, concepts of materiality, reasonableness, good faith and fair dealing, and the possible unavailability of specific performance or injunctive relief, and limitation of rights of acceleration regardless of whether such enforceability is considered in a proceeding in equity or at law. Attorneys at our firm are admitted to the practice of law in the State of New York and we express no opinion as to the laws of any other jurisdiction other than the federal laws of the United States of America and the General Corporation Law of the State of Delaware. We hereby consent to the filing of this opinion as an exhibit to the above-referenced Registration Statement and to the use of our name wherever it appears in the Registration Statement, the Prospectus, <Page> Hewlett-Packard Company February 25, 2002 Page 4 the Prospectus Supplement, and in any amendment or supplement thereto. In giving such consent, we do not believe that we are "experts" within the meaning of such term as used in the Act or the rules and regulations of the Securities and Exchange Commission issued thereunder with respect to any part of the Registration Statement, including this opinion as an exhibit or otherwise. <Table> <Caption> /s/ Wilson Sonsini Goodrich & Rosati Wilson Sonsini Goodrich & Rosati Professional Corporation </Table>