Search code examples
sqlpowershellbcp

SQL BCP within Powershell hangs


I am new to BCP. I am tying to launch bcp.exe utility from Powershell but it hangs. Same command works fine command prompt. I am using Invoke-Expression to launch bcp.exe.

I am able to launch SQLCMD.exe without any problems.

This is my powershell.

Set-Location -Path "C:\Program Files\Microsoft SQL Server\110\Tools\Binn"

SQLCMD.EXE -b -E -S CORPSYSSQLDEV -d CORPSYSDM -Q "select top 10 * from t_test"

$psCommand = "bcp.exe ""testDB.dbo.t_test"" in  ""C:\temp\test\testFile20180919.txt"" -c -t""\t"" -T -S ""TESTSQLDEV"" -e c:\temp\NoahFolder\error.csv"

Write-Host $psCommand

Invoke-Expression $psCommand

This is the result of $psCommand.

bcp.exe "testDB.dbo.t_test" in  "C:\temp\test\testFile20180919.txt" -c -t"\t" -T -S "TESTSQLDEV" -e c:\temp\test\error.csv

Which works fine from command prompt but when I run the powershell script its stuck.

enter image description here

I am able to launch SQLCMD.exe from powershell fine.

What am I doing wrong? Any ideas or pointers.


Solution

  • I should have searched more before asking here. This post helped me. I was able to run BCP from powershell using Start-Process command.

    $bcp = 'c:\Program Files\Microsoft SQL Server\110\Tools\Binn\bcp.exe'
    $arglist = @(
       #add all arguments.
    )
    Start-Process -FilePath $bcp -ArgumentList $arglist