0

The PowerShell part of the syntax I have added works fine when executing it in PowerShell on Windows 10, but when I use it in SQL Server:

Exec xp_cmdshell 'powershell.exe Get-Content "c:/temp/test1.csv" |Select-Object -Skip 1 | Out-File "c:/temp/test2.csv"'

I get the error output

'Select-Object' is not recognized as an internal or external command

I assume this is a simple syntax question :)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ej1
  • 1
  • 1
  • 1
  • I have no experience with `xp_cmdshell`, but a common pitfall with PowerShell commands that should be ok but still generate an error is when you copy and paste them from e.g. Microsoft applications as Word and Outlook that tend to change dashes to an **en-dash** or an **em-dash** (you can hardly distinguish them). In other words: try to re**type** the command. – iRon Jul 04 '19 at 18:26

1 Answers1

0

Try this syntax:

Exec xp_cmdshell 'powershell.exe -Command { Get-Content -Path "c:\temp\test1.csv" | Select-Object -Skip 1 | Out-File -FilePath "c:\temp\test2.csv" }'
mhu
  • 17,720
  • 10
  • 62
  • 93