Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While using Export-Csv in powershell, how to exclude #TYPE Selected.System.Management.ManagementObject from the output

I am using the following PowerShell script to obtain Name, DisplayName, State, StartMode and PathName of all windows services of a local machine and then export the output to a csv file using the Export-csv cmdlet,

Get-WmiObject win32_service | Select Name, DisplayName, State, StartMode, PathName | Export-Csv C:/ListOfServices.csv

the script works fine but the problem is, the first row of the output csv file contains

#TYPE Selected.System.Management.ManagementObject

Is there any way to exclude this line from the output? I am preparing a script to get all these details from all server machines in the network, so excluding this line becomes important.

like image 384
Abilash A Avatar asked Jul 07 '14 07:07

Abilash A


People also ask

How do I Export PowerShell results from CSV?

To do this we can use the Export-CSV function in PowerShell. The Export-CSV function converts PowerShell objects into a CSV string and saves them into a CSV file. If you only need a CSV string, then you can also use the ConvertTo-CSV function in PowerShell.

What is NoTypeInformation in PowerShell?

The NoTypeInformation parameter removes the #TYPE information header from the CSV output and is not required in PowerShell 6. The output shows that the file is not written because access is denied. The Force parameter is added to the Export-Csv cmdlet to force the export to write to the file.

What is the difference between ConvertTo-CSV and Export-CSV commands?

Export-CSV is similar to ConvertTo-CSV , except that it saves the CSV strings to a file. The ConvertTo-CSV cmdlet has parameters to specify a delimiter other than a comma or use the current culture as the delimiter.


2 Answers

Add the -NoTypeInformation switch in your Export-CSV command, like this:

Get-WmiObject -Class win32_service |
Select Name, DisplayName, State, StartMode, PathName |
Export-Csv -NoTypeInformation -Path C:/ListOfServices.csv
like image 149
Frode F. Avatar answered Sep 30 '22 21:09

Frode F.


you need to include -NoTypeInformation in export-csv command to avoid

TYPE Selected.System.Management.ManagementObject

ex: Export-Csv -Path "your path for the export file" -NoTypeInformation

like image 35
Pradeep Avatar answered Sep 30 '22 20:09

Pradeep