Powershell

How to Use the ConvertTo-Csv (Microsoft.PowerShell.Utility)?

The “ConvertTo-Csv” cmdlet is utilized for the conversion of “.NET” objects into the series of CSV strings. It returns the objects that were submitted by the user. It can then be utilized to recreate the objects from a CSV string. This cmdlet does not save the strings to a file like the “Export-Csv” cmdlet. However, it displays the values separated by a comma in the console.

This tutorial will overview the “ConvertTo-Csv” cmdlet in detail.

How to Use the ConvertTo-Csv (Microsoft.PowerShell.Utility)?

As it is described above, the “ConvertTo-Csv” cmdlet .NET objects into the CSV strings and outputs in the PowerShell console.

Example 1: Use the “ConvertTo-Csv” Cmdlet to Convert to CSV with Quotes

Execute the below command to convert the output of the “Get-Date” cmdlet into CSV:

Get-Date | ConvertTo-Csv -UseQuotes AsNeeded

 
In the above-given command:

    • First, specify the cmdlet “Get-Date” and the “|” pipeline.
    • Then, write the “ConvertTo-Csv” cmdlet and the “-UseQUotes” parameter.
    • Lastly, mention the “AsNeeded” cmdlet at the end:

 

Example 2: Use the “ConvertTo-Csv” Cmdlet to Convert the Specific Terms into CSV Within Quotes

To enclose the specific terms within double inverted quotes after converting the output of a specific cmdlet, simply use the “-QuotesFields” parameter and assign it the terms separated by a comma:

Get-Date | ConvertTo-Csv -QuoteFields "DateTime", "Date"

 

Example 3: Use the “ConvertTo-Csv” Cmdlet to Convert a DateTime Object to CSV

Execute the below-provided command to convert a DateTime object to CSV:

$var_Date = Get-Date
ConvertTo-Csv -InputObject $var_Date -Delimiter ';'

 
According to the above-given command:

    • First, initialize a variable and assign it the “Get-Date” cmdlet.
    • Then, write the “ConvertTo-Csv” cmdlet and the “-InputObject” parameter.
    • After that, specify the initialized variable along with the “-Delimiter” parameter having the ‘;’:

 

Example 4: Use the “ConvertTo-Csv” Cmdlet to Convert an Object to CSV

Run the following command to convert a specific object into a CSV:

Get-Process -Name explorer | ConvertTo-Csv

 
Following the above code:

    • initially, specify the cmdlet “Get-Process”.
    • Then, mention the “-Name” parameter having the “explorer” value along with the “|” pipeline.
    • Lastly, add the “ConvertTo-Csv” cmdlet:

 

That’s it! We have briefly discussed the “ConvertTo-Csv” cmdlet in PowerShell.

Conclusion

The “ConvertTo-Csv” cmdlet is used to convert objects into “CSV” string values in PowerShell. It does not store the strings in a file but it displays the string values into the console. This particular post has demonstrated the cmdlet “ConvertTo-Csv” with the aid of various examples.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.