Today when I try to connect to Office 365 on my new machine I encounter this error, which is “Import-PSSession: Files cannot be loaded because running scripts is disabled on this system”. I did some research and checked that my execution policy is restricted.
Import-PSSession: Files cannot be loaded because running scripts is disabled on this system. At line:3 Char:1 +Import-PSSession $Session -DisableNameChecking +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo : InvalidOperation: (:) [Import-PSSession], PSInvalidOperationExc eption - FullyQualifiedErrorId : InvalidOperations,Microsoft.PowerShell.ImportPSSession Command
To resolve this issue we need to run Set-ExecutionPolicy Unrestricted -Force
When I run this Set-ExecutionPolicy Unrestricted -Force command, I encounter this error. Any idea why?
Because I was not running as an administrator. So I restarted my PowerShell in Administrator mode, then ran the same command and it runs successfully.
After running “Set-ExecutionPolicy Unrestricted -Force ” I again tried to connect to Office 365, now this time I haven’t face any issue and I am connected to Office 365 successfully.
If you want to work with Powershell, then the first step is to set your execution policy in a correct way, you can set execution policy in 4 different way – Restricted, Unrestricted, RemoteSigned and AllSigned. Now if your Execution policy is too strict, then you can’t able to run any execution scripts. And if its open to all then, you left your system or environment-wide open for security attackers.
Here’s the list of Execution Policy parameters for PowerShell with detailed explanations of each:
Restricted– Restricted policy is the default execution policy during or after the installation of PowerShell. When this execution policy is in effect, then all execution scripts are disabled. You can still use PowerShell but as an interactive command interpreter. The restricted policy is the most secure execution policy, the user has very limited ability to use PowerShell for automation.
AllSigned– When an AllSigned policy is executed then the user can execute scripts, but they must be in the Authenticode-signed state before they run or execute any script. This AllSigned execution policy allows all PowerShell scripts to run if they have been digitally signed by a trusted publisher.
RemoteSigned – When an AllSigned policy is executed then you can only those scripts which are created by you. If you downloaded any script from the internet and wants to run that downloaded script then sorry you are blocked from that. This RemoteSigned execution policy is best for those who have only one IT admin in the organization. If you want to run the script which is downloaded from the internet then you need to Authenticode-signed before you want to execute that script.
Unrestricted– When an AllSigned policy is executed then You can run any script. Even when you are using Unrestricted execution policy, still it will prompt the user when it encounters a script which has been downloaded from the internet.