PowerShell ConvertTo-CSV

PowerShell ConvertTo-CSV

Let me say right off the bat that wherever possible I prefer to use Export-Csv.  This because I usually want to view the results in Excel, rather than to study them at PowerShell’s command line.

Topics for PowerShell’s ConvertTo-CSV

 ♣

Introduction to ConvertTo-CSV

Let us use the output from Get-Service as a vehicle to test ConvertTo-Csv. The idea is to find services that are running (and not stopped), furthermore, let us see if they "CanStop" or "CanShutdown".

$Filter ="Name", "Status", "CanStop", "CanShutdown"
Get-Service | Where-Object {$_.status -Match "Running"} `
| Select-Object $Filter

Note 1: This is just to create a stream of services.

Example 1: Simple ConvertTo-Csv Cmdlet

The scripting purpose of this script is to change the output to csv format.

Clear-Host
$Filter ="Name", "Status", "CanStop", "CanShutDown"
Get-Service | Where-Object {$_.status -Match "Running"} `
| Select-Object $Filter | ConvertTo-Csv

Note 2: This script returns comma-separated, variable-length (CSV) strings that represents the service objects.  However, I would still prefer to use Export-Csv.

Research Parameters for ConvertTo-Csv

# ConvertFrom-Csv Parameters
Get-Help ConvertTo-Csv -Full

Note 3: PowerShell’s help reveals useful parameters such as -NoTypeInformation; you can also replace the comma with other characters thanks to -Delimiter.

Guy Recommends:  SolarWinds’ Free Bulk Import ToolFree Download Solarwinds Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD’s attributes, click and import the users.

Optionally, you can provide the name of the OU where the new accounts will be born. Download your FREE bulk import tool.

If you need more comprehensive application analysis software,
Download a free trial of SAM (Server & Application Monitor)

Example 2: ConvertTo-Csv Change Delimiter

Clear-Host
$Filter ="Name", "Status", "CanStop", "CanShutDown"
Get-Service | Where-Object {$_.status -Match "Running"} `
| Select-Object $Filter | ConvertTo-Csv -NoTypeInformation -Delimiter "-"

Note 4: You can use almost any keyboard character as a delimiter, for example, "#" or "=".  However, it makes little sense to use alphabetic characters!

Review SolarWinds Free CSVDE Import Tool »

Discover Similar Convert Cmdlets

PowerShell has a whole family of Convert parameters, this is how you can get a list:

Clear-Host
Get-Command -Verb Convert* | Format-Table Name

Results:

      Name
      ———————-

ConvertFrom-Csv
ConvertFrom-Json
ConvertFrom-SecureString
ConvertFrom-StringData
Convert-Path
ConvertTo-Csv
ConvertTo-Html
ConvertToDateTime (ScriptMethod)
ConvertTo-Json
ConvertTo-SecureString
ConvertTo-WebApplication (Converts an IIS virtual directory to an IIS Web application.)
ConvertTo-Xml

See also honorary member Export-CliXml »

Summary of PowerShell’s ConvertTo-Csv Cmdlet

While I prefer to use Export-Csv, there are PowerShell tasks which call for manipulating the output in csv format.  ConvertTo-Csv returns comma-separated, variable-length (CSV) strings that represents the objects inputted.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell output tutorials:

PShell Home   • Out-GridView   • PowerShell Splatting   • Read-Host   • Write-Host   • ConvertTo-Html

Export-CSV   • Import-CSV   • PowerShell Write-Progress   • PowerShell Measure-Command

PowerShell 3 -PassThru   • PowerShell 3.0 Redirection   • ConvertFrom-Csv   • Free CSV Import Tool

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.