Export vs. ConvertTo - what's the difference?

This topic contains 2 replies, has 2 voices, and was last updated by  Ramon Tan 2 weeks, 1 day ago.

  • Author
    Posts
  • #101863

    Ramon Tan
    Participant

    I am a beginner to PS and am enjoying the book by Jones & Hicks (am now past Ch. 8), especially the exercises ("LABS").
    I have noted to myself that while there are "twin" cmdlets such as: ConvertTo-CSV / ConvertFrom-CSV,
    this "duality" does not exist for: ConvertTo-HTML, ConvertTo-XML — meaning 'To's exist but there are none for 'From's.
    I think I know why, given the meanings (or nature) of CSV versus HTML and XML files, but that is not the reason for my post.
    I still do not fully understand the fundamental difference between:

    – ConvertTo-CSV versus Export-CSV,
    – Export-EXCEL versus Export-CSV << on this second pair, all I know is one is a cmdlet, the other a function. the HELP and ABOUT files files don't seem to explain much. Would be grateful for any tips or pointers or references to existing docs/websites that can explain these differences better. Thanks in advance!

  • #101866

    Rob Simmers
    Participant

    Export-* typically results in an export to a file in that format.

    #Export services to a CSV file
    Get-Service | Export-CSV -Path C:\Scripts\Services.csv -NoTypeInformation
    

    Convert-* is just converting a PSObject into the requested format. For instance, you might take data and convert it to JSON to pass it to a web service\API.

    #Get the first 3  services Name and Status and convert it JSON
    PS C:\Users\Rob> Get-Service | Select Name, Status -First 3 | ConvertTo-Json
    [
        {
            "Name":  "AJRouter",
            "Status":  1
        },
        {
            "Name":  "ALG",
            "Status":  1
        },
        {
            "Name":  "AppIDSvc",
            "Status":  1
        }
    ]
    
    • #101872

      Ramon Tan
      Participant

      Much thanks, Mr. Rob Simmers.
      Your inputs are very helpful to me.
      Best,

You must be logged in to reply to this topic.