Only line by line working, run script fail

Welcome Forums General PowerShell Q&A Only line by line working, run script fail

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #109628

    Participant
    Points: 0
    Rank: Member

    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

     

  • #109706

    Participant
    Points: 894
    Helping Hand
    Rank: Major Contributor

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

  • #109814

    Participant
    Points: 0
    Rank: Member

    Thanks.!Got it!

The topic ‘Only line by line working, run script fail’ is closed to new replies.