Author Posts

August 23, 2018 at 7:16 am

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

 

August 23, 2018 at 7:40 am

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).

August 24, 2018 at 1:42 am

Thanks.!Got it!