Category filter

Script to create a CSV file on Windows devices

A comma-separated value (CSV) file is a plain text file where data can be stored in a structured manner and can be opened in text editors, spreadsheet programs like Excel, or other specialized applications. If the IT admin of an organization wishes to create and add content to CSV files on their Windows devices, they can use PowerShell or batch scripts. The scripts can be remotely deployed to the endpoints using Hexnode UEM’s Execute Custom Script action.

Disclaimer:

The sample scripts provided below are adapted from third-party open-source sites.

Batch script

Create a CSV file

In this script, you can specify the desired output path via the outputPath variable. Replace the CSV file path with the actual path where you want the CSV file to be created.

echo commands are used to write the contents of the CSV file, and the output is redirected to the specified outputPath using the > symbol. Using the script for an existing file overwrites that file.

Append data to a CSV file

The newData variable should contain the new data you want to append to the existing CSV file. Make sure to follow the CSV format, where each value in a row is separated by a comma. Modify Value1A, Value1B, Value1C with the desired content.

The echo command with the >> redirection appends the value of newData as a new row at the end of the CSV file specified by csvFile.

PowerShell script

Create a CSV file

pscustomobject creates a structured data object, where the object is a row in a CSV file. Field 1, Field 2, and Field 3 are the property values/column headers of the object. The objects are stored in the $Object variable, from where they are sent down the pipeline to the Export-Csv cmdlet. Export-Csv converts these objects to CSV (comma-separated value) strings to create the file, which is then exported to a location specified by the Path parameter. To remove the first line in the CSV file where PowerShell stores information about the file (Type Information header), use the NoTypeInformation parameter. Using Export-Csv for an existing file overwrites the file.

For example, to create a CSV file named Students listing information on the first name, last name, and email IDs of students in a class, the script would look like this:

$content = @(

[pscustomobject]@{

FirstName = 'James'

LastName = 'Stewart'

EmailID = 'jamey16@gmail.com'

}

[pscustomobject]@{

FirstName = 'John'

LastName = 'Oliver'

EmailID = 'twist11@gmail.com'

}

[pscustomobject]@{

FirstName = 'Howard'

LastName = 'Stern'

EmailID = 'Stern415@gmail.com'

}

)

$content | Export-Csv -Path D:\Students.csv -NoTypeInformation

Append data to a CSV file

Use the Append parameter to add rows to an existing file.

E.g., To add information about a new student to the Students CSV file:
[pscustomobject]@{ FirstName = 'Michael'; LastName = 'Mann'; EmailID = 'mmann214@gmail.com' } | Export-Csv -Path D:\Students.csv -Append -NoTypeInformation

Notes:

  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository