Hi @Christian Bahnsen -- .mil account ,
you have to define the specify the Data Table columns before entering data to the Data Table.
Maybe this helps:
# Define the DataTable Columns
$table = New-Object system.Data.DataTable 'TestDataTable'
$newcol = New-Object system.Data.DataColumn FirstName,([string]); $table.columns.add($newcol)
$newcol = New-Object system.Data.DataColumn LastName,([string]); $table.columns.add($newcol)
$newcol = New-Object system.Data.DataColumn ID,([int]); $table.columns.add($newcol)
# Add a DataTable row
$row = $table.NewRow()
$row.FirstName= ("Bill")
$row.LastName= ("Gates")
$row.ID= [int](123456)
$table.Rows.Add($row)
# Get data from csv
$file = "./Junk/user.csv"
$users = Import-CSV $file -delimiter ";"
$users | ForEach-Object {
$row = $table.NewRow()
$row.FirstName= ($_.FirstName)
$row.LastName= ($_.LastName)
$row.ID= [int]($_.ID)
$table.Rows.Add($row)
}
# Get the data out
$table
And here the CSV file content:
FirstName;LastName;ID
Steve;Ballmer;12346
Lucky;Luke;12347
Peter;Pan;12348
----------
(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)
Regards
Andreas Baumgarten