Galen H
Galen H

Reputation: 65

Powershell 3.0: After I save a working script, if I close and then reopen the script I receive an error message when I try to run it

I just wrote this code:

Get-ChildItem 'R:\Source\Path' |
 Where-Object { $_.Name -notlike '*_*' } |
 Select-Object -ExpandProperty Name > 'C:\Destination\Path\Test.txt' |
 Clear-Host
 Invoke-Item 'C:\Destination\Path\Test.txt'

which worked perfectly. I saved the code and then closed power shell. When I reopened powershell and ran this specific program, it produced the following error message:

File C:\Users\Galen\Powershell Code\InvokeItem.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170. + CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnauthorizedAccess

I am the administrator on this computer. What happened?

Upvotes: 0

Views: 1405

Answers (1)

Vinay
Vinay

Reputation: 1034

What is your ExecutionPolicy set to ?

PS C:\Users\Me\Desktop> ExecutionPolicy

If it's Restricted , then it runs PowerShell in Interactive mode only which is why it ran before.

If you are an admin, then try setting it to Unrestricted and execute your script

PS C:\Users\Me\Desktop> Set-ExecutionPolicy Unrestricted

More Info - Using the Set-ExecutionPolicy

Upvotes: 1

Related Questions