Skip to content

Instantly share code, notes, and snippets.

@cbaragao
Last active April 20, 2025 03:37
Show Gist options
  • Select an option

  • Save cbaragao/36d44b7163b8b489619510e8c5841726 to your computer and use it in GitHub Desktop.

Select an option

Save cbaragao/36d44b7163b8b489619510e8c5841726 to your computer and use it in GitHub Desktop.
Import-Module PSDuckDB
# Start timing
$startTime = Get-Date
# Parameters
$table = "test_table"
$sql = "SELECT * FROM $table"
$output = "Output\QueryResults.csv"
# Create a connection
$db = New-DuckDBConnection
# Change this to your directory path
$directory = "Data"
# Get the CSV files in the directory - optional filter for specific files
$csvFiles = Get-ChildItem -Path $directory -Filter *.csv #| Where-Object { $_.Name -like "*customer*" }
# Build the CSV file paths for the DuckDB query
$csvPaths = $csvFiles.FullName -join "', '"
# Create a view for the table
$db.sql("CREATE OR REPLACE VIEW $table AS SELECT * FROM read_csv_auto(['$csvPaths']);")
# Get a record count
$db.sql("SELECT COUNT(1) AS record_count FROM $table;")
# Delete the file if it exists
if (Test-Path $output) {
Remove-Item $output -Force
}
# Export the results to a CSV file
$db.sql("COPY ($sql) TO '$output' (HEADER, DELIMITER ',', ENCODING 'UTF8');")
#Close the in-memory database connection
$db.CloseDb()
# Display the duration
$endTime = Get-Date
$duration = $endTime - $startTime
# Notify the user
Write-Host "Results saved to $output. Completed in $($duration.TotalSeconds) seconds." -ForegroundColor Blue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment