• Thanks GSC.

    Even I am using the below one. It worked for few days, but now I find that it hangs in the server. I used this code in job step 2, which ran for 4 days and I killed that today in the morning. Not sure, what happened.

    DECLARE @pscmd varchar(1000)

    DECLARE @targetpath varchar(8000)

    DECLARE @olddays int

    DECLARE @extension varchar(5)

    DECLARE @cmdstring varchar(1000)

    SET @targetpath = 'D:\bkp\data\output_results';

    SET @olddays = -30 --pass the days with negative values

    --SET @extension = 'txt'

    SET @pscmd = '"& '+

    'Get-ChildItem ' + Quotename(@targetpath,'''') + '-Recurse' + ' | '+

    'where {$_.lastWriteTime -lt ((Get-Date).AddDays('+ CAST(@olddays as varchar) +')) '+

    --'-and ($_.Extension -match ' + QUOTENAME(@extension,'''')+ ') } | ' +

    --'-and ($_.Extension -match ' + ') } | ' +

    '} |' + 'Remove-Item -force " '

    SET @cmdstring = ' ""powershell.exe" '+ @pscmd

    select @cmdstring

    exec master..xp_cmdshell @cmdstring, no_output

    Thanks.