Only line by line working, run script fail

Hi all, I am beginner of powersheel, I need to clear the file of my server automically by date (14 days), and strange is I think the code is fine I using Powersheel ISE and can run the code line by line working perfect(f8). But if I run the script (f5), i got “cannot be loaded because running script is disable on the system” My code is as below.

Any idea? Thanks.

Set-ExecutionPolicy unrestricted -force
Unblock-File -PATH “C:\temp\texst.ps1”
Get-ChildItem -Path “\My network path” | where {$_.LastWriteTime -le $(get-date).AddDays(-14)} | Remove-Item
Set-ExecutionPolicy restricted -force


F5 in ISE requires, the script to be saved and running a script is restricted by default as you execution policy is restricted. you have to Set it before running the script and not within the script.

Open a PowerShell (with elevated permission) and run

Set-ExecutionPolicy RemoteSigned -force

I suggest you to take a look at the documentation of about_execution_policies

and when you post code in the forum, use code posting tag(pre).

Thanks.!Got it!