This question already has an answer here:

I have a powershell script which deletes old backups from NAS storage. I have already tested it using Windows Powershell and it works but I couldn't perform the same thing in SQL Server. I have created a cmdExec type job and then wrote the script below (script is on desktop):



".\deleteold.ps1 -FolderPath \\192.168.x.x\Backups -FileAge 2 -ListOnly -LogFile \\192.168.x.x\Backups\log.log"

But I do not get any result as when I run the job it just keeps running infinitely until I stop that job.

marked as duplicate by LowlyDBA, Marcello Miorelli, McNets, Vérace, John Eisbrener Sep 9 at 14:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Not sure how the 3 lines in your question are being used. A one-liner should work as long as the execution account has access to the referenced script and UNC paths: powershell -ExecutionPolicy RemoteSigned -File "C:\Users\domainAccount\Desktop\\deleteold.ps1" -FolderPath "\\192.168.x.x\Backups" -FileAge 2 -ListOnly -LogFile "\\192.168.x.x\Backups\log.log" – Dan Guzman Sep 8 at 14:27

You need to pass the powershell file in as an argument

powershell.exe -file C:\Users\domainAccount\Desktop\deleteold.ps1 -FolderPath \\192.168.x.x\Backups -FileAge 2 -ListOnly -LogFile \\192.168.x.x\Backups\log.log

Or if the step type doesn’t have to be CmdExe you can change the type to PowerShell

  • I would avoid using the built-in PowerShell step type. It doesn't use the latest version of PowerShell and I've found it to be far more trouble than it's worth. Just save it as a ps1 file and then invoke powershell.exe directly through a cmdexec step. – Jonathan Fite Sep 9 at 12:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.