Virtual Geek

Tales from real IT system administrators world and non-production environment


July 31, 2016 07:07PM

After installing VMware vSphere PowerCLI, when running it for first time you may see below error on the PowerCLI console.

. : File C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1
cannot be loaded because running scripts is disabled on this system. For more information, see
about_Execution_Policies at
At line:1 char:3
+ . "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scri ...
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

Powercli cannot be loaded because running scripts is disabled on this computer

This is due to when we run PowerCLI it try to execute initialize-powerCLIEnvironment.PS1 kept under %program files%\VMware\Infrastructure\vSphere Powercli\Scripts but it fails due to by default powershell scripts PS1 cannot be executed and it is blocked, I can confirm this by running command Get-ExecutionPolicy and I get result as Restricted. Due to this Powercli vmware related commands cannot be executed and you see error. (I am running one of the Get-VM vmware command) 

get-vm : The term 'get-vm' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ get-vm
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (get-vm:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

get-vm : The term

To resolve this open Powershell - Run as administrator and execute below command and type Yes to enable script file execution.

Set-ExectionPolicy Unrestricted

Powershell Set-Executionpolicy unrestricted -force

Next just relaunch Powercli and PowerCLI command environment will be available. and you can use connect-viserver to login into vcenter or Esxi host.

Powercli environment connect-viserver

Go Back