Agent, CmdExec, PowerShell, BCP, and SSIS Explained with Real-World Guidance
Introduction
Exporting data from SQL Server sounds easy until you try to automate it.
Most DBAs eventually face the same request:
“Run this stored procedure weekly and drop a CSV on a file share.”
That request turns into:
- job failures
- permission issues
- Agent context confusion
- PowerShell syntax errors
- path resolution problems
- arguments not recognized
- scripts that work manually but fail in Agent
The problem is not SQL Server.
The problem is that there are multiple ways to export data, and they all behave differently depending on context.
This article explains every common export method, what each one is good at, what breaks in production, and what is actually recommended in real environments. You’ll also get a step-by-step workshop that works, based on hard lessons most DBAs learn the painful way.
The Export Options (and When to Use Them)
1. T-SQL Output from SQL Server Agent
Not recommended for production
SQL Server Agent allows you to redirect query output to a file. This works for quick testing but has serious limitations.
Pros
- Fast to set up
- No external tools
- Works for tiny result sets
Cons
- Poor formatting
- No headers
- No control
- Hard to troubleshoot
- Breaks easily
Recommendation
Only use this for ad-hoc testing. Not for scheduled jobs.
2. BCP (Bulk Copy Program)
Best for large tables, not stored procedures
BCP is fast, simple, and reliable when exporting tables or views.
bcp "SELECT * FROM dbo.Table" queryout "\\share\file.csv" -c -T -S Server
Pros
- Extremely fast
- Handles large datasets
- Simple syntax
- Minimal overhead
Cons
- Awkward for stored procedures
- No easy formatting
- Requires staging tables for SP output
Recommendation
Use BCP for tables or views, not for stored procedures.
Microsoft documentation:
https://learn.microsoft.com/en-us/sql/tools/bcp-utility
3. SQL Server Agent PowerShell Step
Recommended for most use cases
This is the most flexible and safest approach.
PowerShell allows you to:
- call stored procedures
- control file names
- handle errors
- log output
- retry logic
- format CSVs properly
- test outside Agent easily
Example:
Invoke-Sqlcmd ` -ServerInstance "SERVER" ` -Database "DB" ` -Query "EXEC dbo.usp_export" |Export-Csv "\\share\file.csv" -NoTypeInformation
Pros
- Best control
- Easy debugging
- Works with stored procedures
- Production-friendly
- Readable
Cons
- PowerShell syntax must be correct
- Agent context matters
- Paths and quoting matter
Recommendation
This is the default choice for most teams.
4. CmdExec + PowerShell
Good when PowerShell step causes issues
Sometimes PowerShell job steps fail due to Agent quirks or environment constraints. In those cases, CmdExec is more predictable.
Example:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -File "C:\Scripts\Export.ps1"
Pros
- Very reliable
- Clear execution context
- Easy to debug
- No Agent PowerShell weirdness
Cons
- Slightly more verbose
- Still need correct quoting
Recommendation
Use CmdExec if PowerShell step gives you trouble. It is 100% valid.
5. SSIS
Best for enterprise ETL pipelines
SSIS is designed for data movement at scale and across systems.
Pros
- Logging
- retry logic
- transformations
- error handling
- enterprise-ready
Cons
- Overkill for simple exports
- More maintenance
- Requires SSIS knowledge
Recommendation
Use SSIS when export is part of a larger ETL pipeline, not for simple file drops.
Microsoft docs:
https://learn.microsoft.com/en-us/sql/integration-services
What Breaks Most Often (Real World)
Based on real DBA pain, these are the top failures:
- Wrong execution context (Agent account vs user account)
- Wrong file path
- Wrong quoting
- PowerShell syntax errors
- Script runs manually but not in Agent
- Path with spaces not quoted
- Using PowerShell step and CmdExec syntax together
- Using Agent PowerShell step but calling powershell.exe inside it
- Script exists but Agent cannot read it
- File share permissions missing
None of these are SQL issues. They are context issues.
What Is Actually Recommended
For most environments:
SQL Agent + PowerShell script + CmdExec
Why?
- easiest to test
- easiest to debug
- least fragile
- works everywhere
- future-proof
Full Workshop (Guaranteed to Work)
Goal
Run a stored procedure weekly and export results to CSV.
Step 1: Create the script
Save this as:
C:\Scripts\ExportPolicies.ps1
Invoke-Sqlcmd ` -ServerInstance "Q-DBCOPERN1" ` -Database "WatchDog" ` -Query "EXEC watchdog.policies_effective_date_select" |Export-Csv "C:\Scripts\policies.csv" -NoTypeInformation
Step 2: Test manually (do this first)
powershell.exe -ExecutionPolicy Bypass -File "C:\Scripts\ExportPolicies.ps1"
If this fails, Agent will fail too. Fix it here first.
Step 3: Create SQL Agent Job
- Step type: Operating system (CmdExec)
- Run as: SQL Agent service account
- Command:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -File "C:\Scripts\ExportPolicies.ps1"
Step 4: Schedule job
- Weekly
- Sunday
- Off-hours
Step 5: Move output to file share (after testing)
Once working locally, change the CSV path:
Export-Csv "\\fileserver\exports\policies.csv" -NoTypeInformation
Final Thoughts
Exporting data from SQL Server is not hard.
Automating it correctly is what trips people up.
The biggest mistake DBAs make is mixing execution models:
- PowerShell step syntax inside CmdExec
- CmdExec syntax inside PowerShell step
- manual tests that don’t match Agent context
Pick one method. Test it manually. Then automate it.
If you remember only one thing:
If it runs manually as the same account, it will run in Agent.
Everything else is just syntax.
References
- Microsoft BCP Utility
https://learn.microsoft.com/en-us/sql/tools/bcp-utility - SQL Server Integration Services
https://learn.microsoft.com/en-us/sql/integration-services - SQL Server Agent Job Steps
https://learn.microsoft.com/en-us/sql/ssms/agent/sql-server-agent-job-steps - PowerShell Invoke-Sqlcmd
https://learn.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd
Discover more from SQLyard
Subscribe to get the latest posts sent to your email.


