-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge-Csv.ps1
More file actions
30 lines (23 loc) · 929 Bytes
/
Merge-Csv.ps1
File metadata and controls
30 lines (23 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
param(
[Parameter(Mandatory=$true)][string]$Path = ".\*.csv",
[Parameter(Mandatory=$true)][string]$OutputFile = "combined-"+(Get-Date -Format "yyyy-MM-dd")+"_"+(Get-Date -format "HHmm")+".csv"
)
$Files = Get-ChildItem -Path $Path
$AllData = New-Object System.Data.DataTable
ForEach ($File in $Files) {
$FileData = Import-Csv -Path $File
$Columns = $FileData.PSObject.Members | Where MemberType -eq NoteProperty | Select -Expand Name
ForEach ($Column in $Columns) {
if (-Not $AllData.Columns.Contains($Column)) {
[void]$AllData.Columns.Add($Column)
}
}
ForEach ($FileDataRow in $FileData) {
$Row = $AllData.NewRow()
ForEach ($AllDataColumn in $AllData.Columns) {
$Row.$AllDataColumn = $FileDataRow.($AllDataColumn.ColumnName)
}
$AllData.Rows.Add($Row)
}
}
$AllData | Export-Csv -Path $OutputFile -NoTypeInformation