How to run VMware vSphere CLI perl scripts from PowerCLI

PowerCLI logoWith the announced VMware vSphere version 5.0, ESX servers will be history. Only ESXi servers are left over. And as you might now, the main difference between ESX and ESXi is the service console that ESX has and is removed from ESXi.

To be able to manage ESXi servers, VMware is promoting the VMware vSphere Command-Line Interface (vCLI). And also the vSphere Management Assistant, which is a Linux virtual appliance with the vCLI installed in it. There is also a Microsoft Windows version of the vCLI. That runs in a Windows Command Prompt.

As a PowerShell user I don´t like writing scripts for the Command Prompt anymore. Wouldn’t it be nice if we could run the vCLI commands from PowerCLI?

To be able to run the vCLI perl scripts from a PowerCLI session, I created a function called Add-vCLIfunction that creates a PowerShell function for every perl script in the vCLI bin directory. In this directory are also the esxcli.exe and w9xpopen.exe programs. Because PowerCLI 4.1U1 contains native esxcli support, I thought it was not necesarry to add a function for esxcli. As far as I know w9xpopen is a program that is used by the perl scripts and not a vCLI command, so I left that out as well.

function Add-vCLIfunction {
    Adds the VMware vSphere Command-Line Interface perl scripts as PowerCLI functions.

    Adds all the VMware vSphere Command-Line Interface perl scripts as PowerCLI functions.
    VMware vSphere Command-Line Interface has to be installed on the system where you run this function.
    You can download the VMware vSphere Command-Line Interface from:

    Adds all the VMware vSphere Command-Line Interface perl scripts as PowerCLI functions to your PowerCLI session.

    VMware vSphere PowerCLI

    Author:  Robert van den Nieuwendijk
    Date:    21-07-2011
    Version: 1.0

  process {
    # Test if VMware vSphere Command-Line Interface is installed
    If (-not (Test-Path -Path "$env:ProgramFiles\VMware\VMware vSphere CLI\Bin\")) {
      Write-Error "VMware vSphere CLI should be installed before running this function."
    else {
      # Add all the VMware vSphere CLI perl scripts as PowerCLI functions
      Get-ChildItem -Path "$env:ProgramFiles\VMware\VMware vSphere CLI\Bin\*.pl" | ForEach-Object {
        $Function = "function global:$($_.Name.Split('.')[0]) { perl '$env:ProgramFiles\VMware\VMware vSphere CLI\bin\$($_.Name)'"
        $Function += ' $args }'
        Invoke-Expression $Function

Figure 1. The Add-vCLIfunction that adds vCLI perl scripts as PowerShell functions.

After you have run the Add-vCLIfunction function, you can use the vCLI perl scripts from your PowerCLI session. The next example uses “vicfg-nics -l” to retrieve all the nics from all the hosts. Although I know that you can do this in PowerCLI also, using the Get-VMHostNetworkAdapter cmdlet, I use this example to show the power of the combination of PowerCLI and vCLI.

# Example how you can use PowerCLI to run VMware vSphere CLI perl scripts
Get-VMHost | ForEach-Object {
  vicfg-nics -l --server $_.Name

Figure 2. Example how you can use PowerCLI to run VMware vSphere CLI perl scripts.

To prevent you from entering the username and password for every vCLI command that you run, you can store them in the environment variables VI_USERNAME and VI_PASSWORD.

The output of the script from figure 2 is shown in figure 3.

vicfg/nics output

Figure 3. Output of the example from figure 2.


The Add-vCLIfunction function adds the power of the VMware vSphere Command-Line Interface to PowerCLI. This combination gives you the best of both worlds.


About Robert van den Nieuwendijk
Robert van den Nieuwendijk is a freelance senior systems engineer with over 30 years of experience in the IT industry. He focusses on VMware vCloud Suite and Microsoft Windows Server. He tries to automate as much of his work as possible using Microsoft PowerShell. Robert is the author of the books “Learning PowerCLI” and “Learning PowerCLI – Second Edition.” Robert is a frequent contributor and moderator at the VMware VMTN Communities. He has a bachelor degree in software engineering and holds the following IT certifications and accreditations: VSP 2016, VTSP 2016, VCP4-DCV, VCP5-DCV, VCP6-DCV, VCP6-CMA, VCA-Cloud, VCA-WM, VCA-NV, VMSP, VMTSP, ZCS, ZCP, ZCP-Cloud, MCSE, MCSA, MCP, MCP+I, PRINCE2 Foundation and ITIL Foundation. In 2012, 2013, 2014, 2015, 2016, 2017, 2018 and 2019 Robert received the VMware vExpert award for his contribution to the community of VMware users over the past year. In 2017 Robert also received the VMware vExpert Cloud award. PernixData made him in 2015 a member of the PernixPro.

12 Responses to How to run VMware vSphere CLI perl scripts from PowerCLI

  1. AlanR says:

    Can you help, this looks great and will do what I need, embed CLI into Powershell. Hoever when I run the Add function it errors with the following

    The term ‘perl’ 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:34
    + function global:vicfg-nics { perl <<<< 'C:\Program Files\VMware\VMware vSphere CLI\bin\' $args }
    + CategoryInfo : ObjectNotFound: (perl:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException.

    I added the .pl; to the $env:PATHEXT with no success. What am I missing?

    • If you install VMware vSphere CLI before starting the PowerCLI session, calling CLI functions from PowerCLI after running the Add-vCLIfunction function should normally work. Because the VMware vSphere CLI installation installs Perl and adds the necessary folders to the PATH environment variable.

      If it still doesn’t work, you should check if the CLI Perl folders are in the PATH environment variable. The needed folders are (for 64 bits Windows):
      C:\Program Files (x86)\VMware\VMware vSphere CLI\Perl\bin
      C:\Program Files (x86)\VMware\VMware vSphere CLI\Perl\site\bin

      or (for 32 bits Windows):
      C:\Program Files\VMware\VMware vSphere CLI\Perl\bin
      C:\Program Files\VMware\VMware vSphere CLI\Perl\site\bin

      If these folders are missing from the PATH environment variable, you can add them using:
      Control Panel – System or My Computer – Properties.

      See for more information about modifying the PATH environment variable.

  2. Nikhil says:


    I am trying to add the vcli functionality to pcli using the above mentioned method. I wonder where i should add the “Add-vCLIfunction” function. Do i have to merge it wih a existing file or create a new file (if so what file name should i give.)

    Nikhil K

    • You can add the Add-vCLIfunction and a call to the function to your PowerShell profile. You can edit your PowerShell profile with:

      Notepad $Profile

      Then the vCLI functions will be available in all your PowerCLI sesions.

  3. Nikhil says:


    Thanks for the post, it was great. When I try to open the PROFILE, it throws the error “the system could not find the path specified”. I have previously edited this file but seems to loose that file now. How can i restore the file. Thanks for the ideas.

    Another question is where should I paste the code implementing the add-vCLifinction as said in the first comment of this thread.

    I want to run esx commands inside esx host using pcli. Is there any pcli command that can be used to invoke commands inside ESX host, as is availble to run inside a VM ( Invoke-VMScript ot Invoke-COmmand). Currently I’m trying to analyse the perl scripts, to know how it works and develop a new script which does ” load and unload drivers inside ESX Host”, which is my requirement.


  4. psvmware says:

    Great stuff Robert !!!!!!!!!!!!!

  5. psvmware says:

    For some reason it did not discover the proper directory for me. I had to change the env:programfiles to ${env:ProgramFiles(x86)} , then it worked. Now i can use all those nice functions. Once again, this is so nice !!!!!!!!!!!!!!!!!!!

    • psvmware says:

      Hmm i wanted to edit my reply but i could not… anyway… another thing to be aware
      $env:VI_PASSWORD=”TopSecret” —-> put ‘ ‘ instead of ” ” 😉

  6. Pingback: Amazing connection, powercli + vsphere cli « VMware i Powershell

  7. Ankit V. says:

    Amazing Work Robert van den Nieuwendijk.
    Thanks for sharing time saving trick. Bit In my case when I am invoking my own created vcli perl script through power shell. Its working good but It is executing again and again It should execute it once only and give control back to power shell. Can you please Guide for the same.
    Any help will be Appreciated.

    function Add-vCLIfunction {
    process {
    # Test if VMware vSphere Command-Line Interface is installed
    If (-not (Test-Path -Path “$env:C:\Program Files (x86)\VMware\VMware vSphere CLI\bin”)) {
    Write-Error “VMware vSphere CLI should be installed before running this function.”
    else {
    # Add all the VMware vSphere CLI perl scripts as PowerCLI functions
    Get-ChildItem -Path “$env:C:\Program Files (x86)\VMware\VMware vSphere CLI\bin\*.pl” | ForEach-Object {
    $Function = “function global:$($_.Name.Split(‘.’)[0]) { perl ‘$env:C:\Program Files (x86)\VMware\VMware vSphere CLI\bin\$($_.Name)'”
    $Function += ‘ $args }’
    Invoke-Expression $Function

    Get-VMHost | ForEach-Object {
    perl -l –server $_.Name

  8. Bonoboblulx says:

    To create a windows powershell profile and make notepad $profile works, go there :

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: