Jump to content

Need help with powershell ps1 file

Hi Guys.

 

I have this code that will delete all my backup file in my NAS which are older than x days

I am running this code directly from powershell and its works fine

 

But my goal is to have this task run from task scheduler via the "ps1" file but it just not working 

Double click on this ps1 also not working

 

I have change my ps1 script to always open with powershell, also set ecexution policy to unresricted (not really sure what this does anyway)

 

here is my script

 

Get-ChildItem -Path "Y:\SM Backup\DB Backup" -File -Recurse -Force | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-5))}| Remove-Item  -Force

 

Can anyone help?

 

Thank you

Link to comment
Share on other sites

Link to post
Share on other sites

Powershell scripts need to be run by right clicking and "run with powershell".

 

To run them via task scheduler, in actions, set it to run powershell.exe and then in "Add arguments" type the below:

 

-f "C:\Scripts\MyPSScript.ps1"

Obviously, replacing that path with the one to your ps1 file.

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Oshino Shinobu said:

Powershell scripts need to be run by right clicking and "run with powershell".

 

To run them via task scheduler, in actions, set it to run powershell.exe and then in "Add arguments" type the below:

 

-f "C:\Scripts\MyPSScript.ps1"

Obviously, replacing that path with the one to your ps1 file.

Problem is even double click on my ps1 file doesnt delete my backup, the powershell  window launch for a few second and just disappear and it doesnt seem to do anything

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, nothin.here said:

Problem is even double click on my ps1 file doesnt delete my backup, the powershell  window launch for a few second and just disappear and it doesnt seem to do anything

Add a line at the end with "pause" written. Will stop it from closing so you can see any errors 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×