SharePoint24x7 It's all about SharePoint.

7Jan/130

Article – Custom Windows PowerShell Scripts and Local Execution Policy

Windows PowerShell supports authoring PowerShell scripts and executing them. You can write Windows PowerShell scripts using any text editor and save them with “.ps1” file extension. You may have encountered the following error when you were trying to execute your PowerShell script and this post explains the story behind this error and how to eliminate it.

I have written a very simple PowerShell script which displays “Hello, World..!” and saved into a script file names “Hello.ps1”.

write-host "====================================="
write-host "Hello, World..!"
write-host "====================================="

When I try to execute the script file using Windows PowerShell command line, I get the following error:

PSError

The reason for this is, once Windows PowerShell is configured in your server, it configures the local execution policy of your server to restricted mode to avoid any attackers to come across the network and execute any PowerShell scripts and harm your environment. Which means, no scripts can be executed without signing your scripts with a Public-Private Key pair.

You can retrieve the current mode of the local execution policy by executing following command:

PS C:\> Get-ExecutionPolicy

localexecutionpolicystatus

Following are the different modes of the local execution policy:

  • Restricted – scripts will not execute under this execution policy without signing with a Public-Private Key pair
  • Unrestricted – scripts can execute under this execution policy. However, the scripts which are not signed will continue to prompt for username and password.
  • Bypass – scripts can execute under this execution policy and it will suppress prompting for username and password

Set the execution policy to unrestricted or bypass by using following command:

PS C:\> Set-ExecutionPolicy unrestricted

Now you can run your script files and they will continue to run without complaining.

Important thing to know when we are working with SharePoint is that, installing SharePoint will loosen the execution policy to unrestricted in all the Application servers and Web Front End servers.

Posted by Joy