This is a PowerShell script that I created when I wanted to combine the data of almost 700 + CSV files into a single CSV file named final1.csv. I will try to explain the process to you in a step by step manner explaining what every step actually does.

STEP 1: Use ‘cd’ to traverse to the folder that contains the multiple files that you want to combine to a single one. Suppose, named final1.csv.

STEP 2: Use a variable named $a for storing all the data i.e only names of all the CSV files that we select using the -property name of the select command. In the following statement all the filtered data of *.csv files are sent through the pipeline where the select statement selects and stores only the names of the particular file into the  $a variable.

$a = dir *.csv| Select -property name

STEP 3: Initialize a variable $i to zero which will be used to loop through the while loop in the next step.

$i=0

STEP 4: Now comes the core part of the script. Since $a is an array of filenames we can get the number of elements stored in the array using $a.count. The while loop is supposed to execute till the value of $i is less than the total number of items stored in the array $a. The $i inside the while loop would display the file number being processed currently. We have used ‘ ; ’ after $i to specially represent the end of statement.

Next we will use Get-Content command with filename ( $a[$ i].name used to access the name of the file stored in the array $a with $i as index ) to get the contents of the Current file.

‘ >> ’ is a redirection operator that would append all the details to the file named final1.csv.

$i++ is used to increment the current value of $i and access the next file thus iterating the while loop.

while($i -lt $a.count){$i;Get-Content $a[$i].name >> final1.csv;$i++}

Now check the directory to see if the file named final1.csv is created with the contents of all other CSV files in it.  This following is the whole script or set of commands.

$a=dir| Select -property name
$i=0
while($i -lt $a.count){$i;Get-Content $a[$i].name >> final1.csv;$i++}

For any queries feel free to comment.