Introduction to PowerShell Export CSV
One of the most effortless ways to put information into an easy-to-read organize is with a comma-separated value (CSV) file. A CSV file can fair be thought of like a spreadsheet without all the chimes and shrieks,it comprises of a line of headers to demonstrate column title and ensuing values for each column all isolated by a comma. There are multiple ways in PowerShell to export data into a csv. The add-content cmdlet can be used to store values in a csv but the most appropriate would be to use the Export-Csv cmdlet.
Syntax and Parameters
Below are the syntax and parameters for PowerShell Export CSV:
Syntax
Export-Csv -InputObject <PSObject> [[-Path] <String>] [-LiteralPath <String>] [-Force] [-NoClobber] [-Encoding <Encoding>] [-Append] [[-Delimiter] <Char>] [-IncludeTypeInformation] [-NoTypeInformation] [-QuoteFields <String[]>] [-UseQuotes <QuoteKind>] [-WhatIf] [-Confirm] [<CommonParameters>]
Export-Csv -InputObject <PSObject> [[-Path] <String>] [-LiteralPath <String>] [-Force] [-NoClobber] [-Encoding <Encoding>] [-Append] [-UseCulture] [-IncludeTypeInformation] [-NoTypeInformation] [-QuoteFields <String[]>] [-UseQuotes <QuoteKind>] [-WhatIf] [-Confirm] [<CommonParameters>]
Parameters
- Append: This parameter denotes that the output needs to be added to the existing data in the file. If this parameter is not used, the contents will be overwritten. The information sort of this parameter is a switch. The default esteem is none. The parameter doesn’t acknowledge pipeline input and wild card characters are moreover not allowed.
- Confirm: This parameter is used for safety purposes. If this cmdlet is used a confirmation will be asked to the user before this cmdlet is run. This will prevent any mishaps from happening. The information sort of this parameter is a switch. Cf is the other way of denoting this parameter. The default esteem is none. The parameter doesn’t acknowledge pipeline input and wild card characters are moreover not allowed.
- Delimiter: This denotes the symbol or character that will be used to differentiate values. For a csv file by default is is a comma(,). Other symbols such as ; or : can also be used. The information sort of this parameter is char. The parameter doesn’t acknowledge pipeline input and wild card characters are moreover not allowed.
- Encoding: This denotes the encoding mechanism to be used while exporting the values to a csv file. The information sort of this parameter is encoding. The parameter doesn’t acknowledge pipeline input and wild card characters are moreover not allowed. The default type of encoding used is UTF8NoBOM. The other values are utf7,utf8,utf32 which denotes encoding in utf7,8 and 32 formats respectively. Ascii, which denotes 7bit character set encoding format. The other types are bigendianunicodes, oem, utf8BOM and utf8NoBOM.
- Force: When there is a need to overwrite an existing csv, this parameter can be used. When force parameter is used with append parameter the unmatched properties are not written to the csv.The information sort of this parameter is switch. The parameter doesn’t acknowledge pipeline input and wild card characters are moreover not allowed. The default value is none for this parameter.
- IncludeTypeInformation: This denotes the datatype of the column and the values that is going to be stored. In other words, the header contains the type.The information sort of this parameter is switch. The parameter doesn’t acknowledge pipeline input and wild card characters are moreover not allowed. The alias of this parameter is ITI.
- Inputobject: This refers the objects or values that needs to be passed to a csv file.The information sort of this parameter is PSObject. The parameter acknowledges pipeline input and wild card characters are moreover not allowed. The esteem value is none for this parameter.
- LiteralPath: This denotes the location where the exported csv will be stored. The path can’t contain wild card characters. This parameter has two aliases PSPath and LP. None is the value of this parameters esteem. The parameter doesn’t acknowledge pipeline input and wild card characters are moreover not allowed.
- NoClobber: When there is a need to not overwrite an existing file, this parameter must be used. If this is not used, the existing file will be replaced without asking for user confirmation. The esteem value for this parameter is none.The parameter doesn’t acknowledge pipeline input and wild card characters are moreover not allowed.
- Path: This parameter is a mandatory one. This denotes the storage location where the file will be stored.The information sort of this parameter is string. The parameter doesn’t acknowledge pipeline input and wild card characters are moreover not allowed. The esteem value is none.
- QuoteFields: This denotes the field name of the columns that needs to be enclosed within double quotes.The information sort of this parameter is string[]. The parameter doesn’t acknowledge pipeline input and wild card characters are moreover not allowed.The esteem value is none. The alias for this parameter is QF.
- UseQuotes: It is used when there is a need for quotation. It can have either of the three values. Never, always and as needed. It’s referred in another way as UQ.The parameter doesn’t acknowledge pipeline input and wild card characters are moreover not allowed. The esteem value is always.
Examples to Implement PowerShell Export CSV
Below are the examples mentioned:
Example #1
Code:
Write-Host "welcome to the example of writing to a csv file"
Write-Host "creating a csv file using add content method"
$location="C:\stduentdetails.csv"
Add-Content -Path $location -Value '"Studentname","Rollno","subject1","subject2","subject3","total","avg","rank"'
$students = @(
'"vignesh","1","90","90","90","270","90","3"'
'"nandhini","2","100","100","100","300","100","2"'
'"vyapini","3","150","150","150","450","150","1"'
)
$students | foreach {
Add-Content -Path $location -Value $_
Write-Host "a row is added to the csv file" -ForegroundColor Green
}
Write-Host "Csv file is created at the location : $($location)" -ForegroundColor Green
Output:
Example #2
Creating and appending to a csv
Code:
Write-Host "Export contents of a directory to csv file"
Get-ChildItem -Path C:\Users\R003646\Desktop\Articles\june -Recurse | Select-Object BaseName, FullName, Name,CreationTime,LastAccessTime,PSDrive | Export-Csv -Path c:\test.csv -Encoding ascii -NoTypeInformation
Write-Host "details are exported to csv, file is created"
Write-Host "display the contents of the csv"
Get-Content -Path c:\test.csv
Write-Host "adding to the file using append"
Get-Service | Select-Object -First 5 | Foreach-Object {
$_ | Select-Object Name, status,DisplayName | Export-CSV -Path C:\test.csv -Append -Force
}
Write-Host "new rows added"
Get-Content -Path c:\test.csv

4.5 (6,349 ratings)
View Course
Output:
Conclusion
Thus, the article covered in detail about export-csv cmdlet in PowerShell. It also explained with few examples how to create a csv file and how to append a new row to it. To learn more in detail it is advisable to write sample scripts and practice them.
Recommended Articles
This is a guide to PowerShell Export CSV. Here we discuss an introduction to PowerShell Export CSV with syntax and parameters and examples. You can also go through our other related articles to learn more –