Thursday, 19 May 2022

PowerShell CSV to DataTable

https://www.dbtales.com/use-powershell-to-import-csv-to-a-datatable/

Powershell to Import CSV To A Datatable

$Datatable = New-Object System.Data.DataTable

## By Default Import-csv uses first row as Header
$csvall = Import-Csv -sdgfsd -Path "C:\Users\user\Documents\orders.csv"

$csvheaders = $csvall | Get-member -MemberType NoteProperty

Foreach ($header in $csvheaders) {   

    $Datatable.Columns.Add($header.Name)
   
}

Foreach($csv in $csvall) {
    
    $row = $Datatable.NewRow()
        $row.DeliveryMethodID = $csv.DeliveryMethodID
        $row.ExpectedDeliveryDate = $csv.ExpectedDeliveryDate
        $row.FullName = $csv.FullName
        $row.IsOrderFinalized = $csv.IsOrderFinalized
        $row.OrderDate = $csv.OrderDate
        $row.PurchaseOrderID = $csv.PurchaseOrderID
        $row.SupplierName = $csv.SupplierName
        $row.SupplierReference = $csv.SupplierReference

    $Datatable.Rows.Add($row)
}

$Datatable | Format-Table 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Blog Archive