Developer ToolsFree generatorPDF, Excel, Word exportInfor VISUALDiscrete Manufacturing

VISUAL SQL to CSV PowerShell Export Script Generator

A scheduled CSV export is the simplest way to feed VISUAL data into Excel, Power BI or another system without touching the application. This generator builds a PowerShell script around Invoke-Sqlcmd with dated file names, error handling and automatic clean up.

Fill in

Generated code
Download:

export-visual.ps1

# Requires the SqlServer module: Install-Module SqlServer
Import-Module SqlServer

$server   = "VISUALSQL01\SQLEXPRESS"
$database = "VMFG"
$folder   = "D:\Exports\Visual"
$prefix   = "released_work_orders"
$keepDays = 30

$query = @'
SELECT BASE_ID, LOT_ID, SPLIT_ID, SUB_ID, PART_ID, STATUS, DESIRED_QTY FROM WORK_ORDER WHERE TYPE = 'W' AND STATUS = 'R'
'@

if (-not (Test-Path $folder)) { New-Item -ItemType Directory -Path $folder | Out-Null }

$stamp = Get-Date -Format "yyyyMMdd_HHmm"
$file  = Join-Path $folder ("{0}_{1}.csv" -f $prefix, $stamp)

try {
    Invoke-Sqlcmd -ServerInstance $server -Database $database -Query $query -QueryTimeout 120 -TrustServerCertificate |
        Export-Csv -Path $file -NoTypeInformation -Encoding UTF8
    Write-Output ("Wrote " + $file)
}
catch {
    Write-Error ("Export failed: " + $_.Exception.Message)
    exit 1
}

Get-ChildItem $folder -Filter ("{0}_*.csv" -f $prefix) |
    Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$keepDays) } |
    Remove-Item

Schedule it (run once in an elevated prompt)

schtasks /Create /TN "Visual Export" /SC DAILY /ST 06:00 /RU SYSTEM /TR "powershell.exe -NoProfile -ExecutionPolicy Bypass -File D:\Scripts\export-visual.ps1"

Use a SQL login with read only rights. The -TrustServerCertificate switch needs a recent SqlServer module; drop it if your module is older. Table and column names in the default query are typical for VISUAL, so confirm them against your version. Test with a small query before scheduling.

Replace scheduled exports with live answers

An optional discussion of a read only natural language layer over your VISUAL database.

Optional. The tool and every export work without this. No spam.

How to use the generator

Enter your SQL Server instance, database name and the query you already tested in SQL Server Management Studio. Choose an output folder and file prefix. The script appears on the right; copy it into a .ps1 file, run it once by hand, then register it with Task Scheduler using the second block. Keep the query on one logical statement and avoid anything that modifies data.

  • Install the SqlServer module first
  • Test by hand before scheduling
  • Use a read only SQL login

What the script does

It creates the folder if needed, stamps the file name with date and time so runs never overwrite each other, runs the query with a timeout, and writes UTF8 CSV through Export-Csv. A try and catch block returns a non zero exit code on failure so Task Scheduler shows the run as failed. The last step removes exports older than the number of days you set, which stops the folder growing without limit.

Common mistakes

The usual problems are quoting and permissions. Single quotes inside the query are safe because the script uses a literal here string, but a line that starts with the closing marker will end the string early. Scheduled tasks run as a different account than you, so the folder and the SQL login must work for that account. Large extracts during shift hours can slow the shop floor, so schedule heavy jobs early morning.

  • Run the task account once interactively to test access
  • Avoid SELECT * on wide tables
  • Schedule large extracts off peak
  • Never store passwords in the script; prefer integrated security

Frequently Asked Questions

Do I need SQL Server Management Studio installed?

No. The script needs PowerShell and the SqlServer module, which provides Invoke-Sqlcmd. Management Studio is only useful for developing and testing the query before you paste it into the generator.

Can this write back to VISUAL?

It is designed for read only extracts. Do not use it to update VISUAL tables, because direct writes bypass application logic, costing and audit trails. Use supported import or API routes for changes.

How do I handle commas and line breaks in data?

Export-Csv quotes fields that contain commas, quotes or line breaks, so Excel reads them correctly. If long text columns cause trouble, trim them in the SELECT with REPLACE on carriage return and line feed characters.

Will it work on SQL Server Express?

Yes. Set the instance name to the server and instance, for example SERVER\SQLEXPRESS. Confirm that remote connections and the SQL Browser service are enabled if the script runs from a different machine.

If you are stitching together many of these exports, we can help replace them with a governed read only data layer over VISUAL.