4

I see lots of examples on running SQLPS from SQL Server Agent, but how do you call a powershell script from TSQL on demand?

I'm replacing a C# CLR function with a PowerShell script. The script will simply get a path and date modified for files in a directory structure.

What's the command to call a PowerShell script from a T-SQL stored procedure?

1 Answer 1

1

Something like this:

set @sql = 'powershell.exe -file "YourScript.ps1" -nologo'

EXEC xp_cmdshell @sql  
Sign up to request clarification or add additional context in comments.

2 Comments

Okay, that worked. Thanks. One more question. I am running the following code : 'powershell.exe (Get-Childitem G:\11764 -include . -recurse) | select LastWriteTime,Name,Directory' The problem is that when I pipe it to the select statement, it causes an error, saying that it isnt a proper command. I'm assuming that it is trying to pipe to a CMD command when it needs to be powershell. Anyway to fix that?
Nevermind, thanks for the help. I fixed that part myself. Just added double quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.