The first step that every PS scripter should do is to set ExecutionPolicy appropriately. If you work at the developer level, where you have to run various scripts/code from other sources and can validated the script actions only then setup it to Unrestricted. Otherwise RemoteSigned is the recommended value.
Setting up PS ExecutionPolicy to to Unrestricted:
You need to run this cmdlet as admin user:
PS C:> Set-ExecutionPolicy Unrestricted
PS C:> Get-ExecutionPolicy
Unrestricted
PS C:>
Otherwise PS will err at you as shown below:
PS C:> Set-ExecutionPolicy Unrestricted
Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution
policy?
[Y] Yes [N] No [S] Suspend [?] Help (default is “Y”): Y
Set-ExecutionPolicy : Access to the registry key ‘HKEY_LOCAL_MACHINESOFTWAREMicrosoftPowerShell1ShellIdsMicrosoft
.PowerShell’ is denied.
At line:1 char:20
+ Set-ExecutionPolicy <<<< Unrestricted
+ CategoryInfo : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyComma
nd
PS C:>
References: