I'm exporting data from Access tables to CSV files using powershell on a Windows 2012 R2 Server. I'd like to do this without modifying the Access database schema and without writing any VBA code in the access database.
The powershell script boils down to something like this:
$Table = 'tblUsers'
$Filepath = 'C:\tblUsers.csv'
$Acc = New-Object -Com Access.Application
$Acc.OpenCurrentDataBase($DBFilePath)
$Acc.DoCmd.Transfertext(2, [Type]::Missing, $Table, $Filepath, 1)
One of the tables has a column with a Number field where the Field Size is Double, Format is Percent, and Decimal Places are set to 2. The field contains values like 100%
, 50%
, and 87.5%
. When I run this command the output in the CSV for a 100% value is ,1.00,
while the output for 87.5%
is ,0.87,
. It seems that something in the process is dropping significant digits of precision for these columns.
Is there a way in the script to ensure I get all the significant digits of every column in the database? Are there other kinds of columns to be worried about?
If it is at all possible, I want a new answer instead of these 2. These 2 answers have significant drawbacks.
By default, if the file exists in the specified path, Export-CSV overwrites the file without warning. Removes the #TYPE information header from the output. This parameter became the default in PowerShell 6.0 and is included for backwards compatibility. A required parameter that specifies the location to save the CSV output file.
Summary: Use Windows PowerShell to round numbers to a specific decimal place. continue to have a number instead of converting it to a string. Use the Round static method from the System.Math class.
Single quotation marks tell PowerShell not to interpret any characters as escape sequences. Use this parameter so that Export-CSV does not overwrite an existing file. By default, if the file exists in the specified path, Export-CSV overwrites the file without warning. Removes the #TYPE information header from the output.
You can pipe any object with an Extended Type System (ETS) adapter to Export-CSV. The CSV list is sent to the file designated in the Path parameter. The Export-CSV cmdlet converts the objects that you submit into a series of CSV strings and saves them in the specified text file.
This code should export the results without any modifications to the Access table or computer settings.
1) It uses Microsoft.Jet.OLEDB.4.0 driver
2) The only limitation is that it must be run via PowerShell x86
, due to this limitation. Alternativly, you can install the driver for 64-bit (link in the post).
This access table (tblFruit
) has 3 fields:
$csvPath = 'C:\temp\output.csv'
$dbPath = 'C:\temp\database1.mdb'
$strQuery = "SELECT * from tblFruit"
$strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$dbPath"
$connection = New-Object -TypeName System.Data.OleDb.OleDbConnection
$connection.ConnectionString = $strConn
$command = $connection.CreateCommand()
$command.CommandText = $strQuery
$adapter = New-Object -TypeName System.Data.OleDb.OleDbDataAdapter $command
$dataset = New-Object -TypeName System.Data.DataSet
$adapter.Fill($dataset)
$dataset.Tables[0] | export-csv $csvPath -NoTypeInformation
$connection.Close()
Output:
"ID","Fruit","Percent"
"1","Apple","0.0142"
"2","Orange","0.3412"
"3","Banana","0.8715"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With