You need to execute the below command if you get the below error while trying to run PowerCLI script for the first time.

$CurrentValue = [Environment]::GetEnvironmentVariable("PSModulePath", "Machine")

[Environment]::SetEnvironmentVariable("PSModulePath", $CurrentValue + ";C:\Program Files (x86)\VMware\Infrastructure\vSphere

powercli error 1

After executing the command

powercli error 2


Below excellent PowerCLI commands were taken from :


To execute the below commands, open PowerCLI and connect to the Vcenter server. Then execute the commands you require.

Upgrade VMware Tools, all virtual machines in cluster:

Get-Cluster "clustername" | Get-VM | Update-Tools –NoReboot

Upgrade VMware Tools, located in folder:

Get-Cluster "clustername" | Get-Folder "Beheer servers" | Get-VM | Update-Tools –NoReboot

Upgrade VMware Tools, selected virtual machine:

Get-Cluster "clustername" | Get-VM "BHR-SVR11" | Update-Tools –NoReboot

Get VMware-Tools versions:

Get-View -ViewType VirtualMachine | select Name, @{ Name=”ToolsVersion”;

Script to get VMware Datastore disk space. Note: This is not my original code yet I have made some changes to it.

AddPsSnapin VMware.VimAutomation.Core
Function Percentcal { 
    [parameter(Mandatory = $true)]
    [parameter(Mandatory = $true)]
    $InputNum1 / $InputNum2*100

$datastores = Get-Datastore | Sort Name 

ForEach ($ds in $datastores) 
    if (($ds.Name -match “Shared”) -or ($ds.Name -match “”))
        $PercentFree = Percentcal $ds.FreeSpaceMB $ds.CapacityMB 
        $PercentFree = “{0:N2}” -f $PercentFree
        $ds | Add-Member -type NoteProperty -name PercentFree -value $PercentFree
$datastores | Select Name,@{N=”TotalSpaceGB”;E={[Math]::Round(($_.ExtensionData.Summary.Capacity)/1GB,0)}},@{N=”UsedSpaceGB”;E={[Math]::Round(($_.ExtensionData.Summary.Capacity – $_.ExtensionData.Summary.FreeSpace)/1GB,0)}},@{N=”FreeSpaceGB”;E={[Math]::Round(($_.ExtensionData.Summary.FreeSpace)/1GB,0)}},PercentFree | Export-Csv c:\DATASTOREREPORT.csv -NoTypeInformation

PowerCLI script to power OFF VMs in Vcenter


Note: Make sure you have VM in the csv file with name at the top as mentioned in the above link

$vcenter="<vcenter name>"  
#Connect to vcenter server  
connect-viserver $vcenter  
#Import vm name from csv file  
Import-Csv E:\Scripts\power\poweredonvms.csv |  
foreach {  
    $strNewVMName = $_.name  
    #Generate a view for each vm to determine power state  
    $vm = Get-View -ViewType VirtualMachine -Filter @{"Name" = $strNewVMName}  
    #If vm is powered on then VMware Tools status is checked  
           if ($vm.Runtime.PowerState -ne "PoweredOff") {  
               if ($vm.config.Tools.ToolsVersion -ne 0) {  
                   Write-Host "VMware tools installed. Graceful OS shutdown ++++++++ $strNewVMName ----"  
                   Shutdown-VMGuest $strNewVMName -Confirm:$false  
                   #For generating email  
                   $Report += $strNewVMName + " --- VMware tools installed. Graceful OS shutdown `r`n"  
               else {  
                      Write-Host "VMware tools not installed. Force VM shutdown ++++++++ $strNewVMName ----"  
                      Stop-VM $strNewVMName -Confirm:$false  
                   #For generating email  
                   $Report += $strNewVMName + " --- VMware tools not installed. Force VM shutdown `r`n"  
write-host "Sleeping ..."  
Sleep 300