Export-Csv

Export a PowerShell object to a comma-separated values (CSV) file.

Syntax
      Export-Csv [[-Path] String] [-LiteralPath String] [-Force]
         [-Append] [-Encoding Encoding] [-IncludeTypeInformation]
            -InputObject PSObject [-NoTypeInformation] [-NoClobber]
               [-QuoteFields String[]] [-UseQuotes QuoteKind]
                  { [[-Delimiter] Char] | [-UseCulture] }   
                     [-WhatIf] [-Confirm] [CommonParameters] 
Key
   -Append
       Use this parameter so that Export-CSV adds CSV output to the end of the specified file.
       Without this parameter, Export-CSV replaces the file contents without warning.
       This parameter was introduced in Windows PowerShell 3.0.

   -Delimiter char
       A delimiter to separate the property values.
       The default is a comma (,). Enter a character, such as a colon (:). 
       To specify a semicolon (;), enclose it in quotation marks.

   -Encoding string
       The encoding for the exported CSV file.
       Valid values are: ASCII, Unicode, UTF7*, UTF8, utf8BOM, utf8NoBOM, UTF32, 
       BigEndianUnicode, bigendianutf32, and OEM.
       Defaults: PowerShell 1-6: ASCII, PowerShell 7+ = UTF8 (NoBOM).
       UTF-7* is no longer recommended 

   -Force
       Overwrite files with the Read Only attribute.

   -InputObject psobject
       The object(s) to export as CSV strings. Enter a variable that contains the
       object(s) or type a command or expression that gets the object(s).

       When the -InputObject parameter is used to submit a collection of items,
       Export-Csv receives one object that represents the collection.
       Because one object cannot be converted, Export-Csv returns the entire collection unchanged.

       To convert multiple items, pipe them to Export-Csv.

       This parameter is an implementation detail: its purpose is to enable input via the pipeline, and its
       direct use with arrays (collections) does not (yet) provide any useful functionality.

   -IncludeTypeInformation
       When this parameter is used the first line of the output contains #TYPE followed by the
       fully qualified name of the object type. For example, #TYPE System.Diagnostics.Process.
       This parameter was introduced in PowerShell 6.0.

   -NoClobber
       Use this parameter so that Export-CSV does not overwrite an existing file.
       By default, if the file exists in the specified path, Export-CSV overwrites the file without warning.

   -NoTypeInformation
       Remove the #TYPE information header from the output. 
       This parameter became the default in PowerShell 6.0 and is included for backwards compatibility.

   -LiteralPath
       Specifies the path to the CSV output file.
       Unlike -Path, the value of the LiteralPath parameter is used exactly as it is typed.
       No characters are interpreted as wildcards. If the path includes escape characters, use single quotation marks.
       Single quotation marks tell PowerShell not to interpret any characters as escape sequences.

   -Path string
       The path to the CSV output file. (required)

   -QuoteFields
       Specifies the names of the columns that should be quoted.
       When this parameter is used only the specified columns are quoted. This parameter was added in PowerShell 7.0.

   -UseCulture
       Use the list separator for the current culture as the data delimiter. 
       Default = comma (,)

       This parameter is very useful in scripts that are being distributed to
       users worldwide. To find the list separator for a culture, use the following:
       (Get-Culture).TextInfo.ListSeparator.

   -NoClobber 
       Do not overwrite (replace the contents) of an existing file.
       By default, Export-CSV will overwrite any existing file without warning.

   -UseCulture
       Use the list separator for the current culture as the item delimiter.
       Without this option, the default separator is a comma (,).

       This is useful in scripts that are being distributed to users worldwide.
       To find the list separator for a culture, use the following command:

         (Get-Culture).TextInfo.ListSeparator.

   -UseQuotes
       Specifies when quotes are used in the CSV files.
       Possible values are:
          Never - don’t quote anything
          Always - quote everything (default behavior)
          AsNeeded - only quote fields that contain a delimiter character
       This parameter was added in PowerShell 7.0.
   
   -whatIf
       Describe what would happen if you executed the command without actually
       executing the command.
       
   -confirm
       Prompt for confirmation before executing the command.

Standard Aliases for Export-CSV: epcsv

Under Windows, Export-CSV will create ASCII files by default. When appending it will partially match the existing encoding of the file, it will correctly match UTF-16LE and UTF-16BE and otherwise assume UTF-8.
This can be configured via the -Encoding option or by setting the $PSDefaultParameterValues preference variable.
Under PowerShell Core edition, the encoding defaults to BOM-less UTF-8

To suppress the header type information, use the -notype parameter.

A function to export to a temporary CSV file and immediately open it in Excel. Pipe anything to this function to open it (script via Idera):

function Out-Excel 
{
  param(
    $path = "$env:temp\report$(Get-Date -Format yyyyMMddHHmmss).csv"
  )
  
  $Input | 
    Export-Csv $path -NoTypeInformation -UseCulture -Encoding UTF8
    Invoke-Item $path 
}

Examples

Select a few properties from the wmiprvse process and export them to a CSV format file:

PS C:> get-process wmiprvse | select-object basePriority,ID,SessionID,WorkingSet |
export-csv -path data.csv

Send a list of files to FileList.csv:

PS C:\> $files = GetChildItem C:\demo\*
PS C:\> $files | Select fullname, lastWriteTime |
export-csv C:\demo\filelist.csv -NoTypeInformation

Exporting a text string (not a PowerShell object) to an ASCII .CSV file can be done with Out-File:

PS C:> Echo "Col1,Col2"`r`n123,456" | out-file demo.csv -encoding ASCII

Export objects representing the processes on the computer to Processes.csv (comma separated):

PS C:> get-process | export-csv E:\processes.csv

Export objects representing the processes on the computer to Processes.csv (semicolon separated):

PS C:> get-process | export-csv E:\processes.csv -Delimiter ";"

Export objects representing the processes on the computer and use -NoTypeInformation to suppress the type information in the output file:

PS C:> get-process | export-csv E:\processes.csv -NoTypeInformation

“Try not to become a man of success but rather to become a man of value” ~ Albert Einstein

Related PowerShell Cmdlets

import-csv - Take values from a CSV list and send objects down the pipeline.
export-clixml - Produce a clixml representation of PowerShell objects.
import-clixml - Import a clixml file and rebuild the PS object.
convertTo-Html - Convert the input into an HTML table.


 
Copyright © 1999-2024 SS64.com
Some rights reserved