
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a CSV file manually in PowerShell?
There are few methods to create a CSV file in the PowerShell, but we will use the best easy method to create it.
First, to create a CSV file in PowerShell, we need to create headers for it. But before it, we need output file name along with its path.
$outfile = "C:\temp\Outfile.csv"
Here we have given output file name Outfile.csv in the path C:\temp.
Now we will create headers,
$newcsv = {} | Select "EMP_Name","EMP_ID","CITY" | Export-Csv $outfile
Here, we have created a CSV file and exported a file to the newly created file.
We will now import this file to check if the headers are created.
Import-Csv $outfile
Output
EMP_Name EMP_ID CITY -------- ------ ----
So, in the above output, we can see the headers are created.
Now we need to insert the data inside the CSV file, for that we first import CSV file into a variable and then we will insert data.
Example
$csvfile = Import-Csv $outfile $csvfile.Emp_Name = "Charles" $csvfile.EMP_ID = "2000" $csvfile.CITY = "New York"
Output
Check the output of the above-inserted data.
PS C:\WINDOWS\system32> $csvfile EMP_Name EMP_ID CITY -------- ------ ---- Charles 2000 New York
Overall commands to create a script −
$outfile = "C:\temp\Outfile.csv" $newcsv = {} | Select "EMP_Name","EMP_ID","CITY" | Export-Csv $outfile $csvfile = Import-Csv $outfile $csvfile.Emp_Name = "Charles" $csvfile.EMP_ID = "2000" $csvfile.CITY = "New York"
This output is stored into the variable, to store this output into the created CSV file, you need to export CSV file to the output file again as shown in the below example.
Example
$csvfile | Export-CSV $outfile
Output
Import-Csv $outfile
Output
EMP_Name EMP_ID CITY -------- ------ ---- Charles 2000 New York
- Related Questions & Answers
- How to Export and import CSV file manually in PowerShell?
- How to convert JSON file to CSV file using PowerShell?
- How to edit the CSV file using PowerShell?
- How to retrieve CSV file headers using PowerShell?
- Importing / Exporting CSV file in PowerShell
- How to append data into a CSV file using PowerShell?
- How to convert JSON to CSV file using PowerShell?
- How to create a blank csv file in R?
- How to create a temporary file using PowerShell?
- How to create dynamic columns (headers) in CSV using PowerShell?
- Python Pandas- Create multiple CSV files from existing CSV file
- How to parse a CSV file using PHP
- How to import csv file in PHP?
- How to read CSV file in Python?
- How to save a Python Dictionary to CSV file?