# 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](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) action.

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

 

Batch script
------------

### Create a CSV file

Batch script to create a CSV file

@echo off set "outputPath=CSV file path" echo Creating CSV file... ( echo Header1,Header2,Header3 echo Value1A,Value1B,Value1C echo Value2A,Value2B,Value2C echo Value3A,Value3B,Value3C ) > "%outputPath%" echo CSV file created at %outputPath%. 

   1

2

3

4

5

6

7

8

9

10

11

12

  @echo off 

set "outputPath=CSV file path"

echo Creating CSV file...

(

echo Header1,Header2,Header3 

echo Value1A,Value1B,Value1C 

echo Value2A,Value2B,Value2C 

echo Value3A,Value3B,Value3C

) > "%outputPath%"

echo CSV file created at %outputPath%. 

   

 

  

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

Batch script to append data to a CSV file

@echo off set "csvFile=Enter path of the CSV file" set "newData=Value4A,Value4B,Value4C" echo Appending data to CSV file... echo %newData% >> "%csvFile%" echo Data appended to the CSV file at %csvFile%. 

   1

2

3

4

5

6

7

8

9

  @echo off 

set "csvFile=Enter path of the CSV file"

set "newData=Value4A,Value4B,Value4C"

echo Appending data to CSV file...

echo %newData% >> "%csvFile%"

echo Data appended to the CSV file at %csvFile%. 

   

 

  

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

PowerShell script to create a CSV file

$Object = @( \[pscustomobject\]@{ Field1 = 'Enter value' Field2 = 'Enter value' Field3 = 'Enter value' } \[pscustomobject\]@{ Field1 = 'Enter value' Field2 = 'Enter value' Field3 = 'Enter value' } \[pscustomobject\]@{ Field1 = 'Enter value' Field2 = 'Enter value' Field3 = 'Enter value’ } ) $Object | Export-Csv -Path <CSV file path> -NoTypeInformation 

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

  $Object = @(

\[pscustomobject\]@{

Field1 = 'Enter value'

Field2 = 'Enter value'

Field3 = 'Enter value'

}

\[pscustomobject\]@{

Field1 = 'Enter value'

Field2 = 'Enter value'

Field3 = 'Enter value'

}

\[pscustomobject\]@{

Field1 = 'Enter value'

Field2 = 'Enter value'

Field3 = 'Enter value’

}

)

$Object | Export-Csv -Path <CSV file path> -NoTypeInformation 

   

 

  

`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

PowerShell script to append data to a CSV file

\[pscustomobject\]@{ Field1 = 'Enter value'; Field2 = 'Enter value'; Field3 = 'Enter value' } | Export-Csv -Path <CSV File Path> -Append -NoTypeInformation 

   1

  \[pscustomobject\]@{ Field1 = 'Enter value'; Field2 = 'Enter value'; Field3 = 'Enter value' } | Export-Csv -Path <CSV File Path> -Append -NoTypeInformation 

   

 

  

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.