Powercli oneliner for changing MTU to 9000

I wanted to set MTU on number of standard vSwitches across multiple ESXi hosts and Powercli made that job easier. Here’s a oneliner to get the job done across multiple vSwitches.

Get-VMHost My-ESXi-Host| Get-VirtualPortGroup "PG_SearchString*" | Get-VirtualSwitch | Set-VirtualSwitch -Mtu 9000 -Confirm:$false




Test existence of a variable in Powershell

Hmmm. Today I was looking up for how to test the existence of a variable a quick google search turned up a solution on stackoverflow.

Here’s the gist of is, you can use the ‘variable PsProvider’ to test the existence of a variable.

Test-Path variable:global:SomeVariable

Read the entire thread over here.

Mentioned here only for my convenience and archival purposes. The entire credit goes to the post on stackoverflow.

Getting Started with PowerCLI

If you haven’t already done so, please go ahead & install PowerCLI from the VMware website. It is simple and straight forward to install PowerCLI. I doubt you should get stuck during installation, but just in case you do, refer HERE or Google around and you should find many articles talking about the installation.

Once you have installed PowerCLI, before we begin, we need to configure PowerShell so that we can execute PowerCLI cmdlets without any difficulty. The default execution policy of PowerShell is “Restricted” which would disallow execution of all PowerShell script files. Now this policy would not allow us to run any PowerCLI cmdlets. So we have to set it to a less obstructive policy which is “RemoteSigned”. This would allow us to run scripts signed by a trusted entity. Here is how we can set the execution policy:

  1. Right Click on Start–>Programs–>Accessories–>Windows PowerShell–>Windows PowerShell
  2. Select “Run as administrator” to start an administrative PowerShell session
  3. On the PowerShell command prompt, type:
Set-ExecutionPolicy RemoteSigned

You can verify if the execution policy was correctly set, by typing the following command:


You can read more about the execution policy by using the Get-Help cmdlet:

Get-Help about_Execution_Policies

Now that’s was more than a page of help. To paginate the output, you can use more function:

Get-Help about_Execution_Policies | more

By now, you should have realized that tab completion works in a PowerShell environment, type initial few characters of a word and press TAB and it will complete the word or cmdlet for you.

Another way to get help would be to use the help command which is actually an alias for the Get-Help.

Help about_Execution_Policies | more

I am assuming you have already installed PowerCLI on your admin desktop. There are a couple of ways to start your PowerCLI session:

  1. Method one, double click on the ‘VMware vSphere PowerCLI’ icon that was created on your desktop and you should be started.
  2. Alternative method would be, at the PowerShell command prompt, type:
Add-PSSnapin VMware.VIMAutomation.Core

This would load the PowerCLI Snap-in, and in turn allow you to access the vSphere PowerCLI cmdlets.

You can verify if your Snap-in is loaded by using the Get-PSSnapin cmdlet.

Get-PSSnapin VMware.VIMAutomation.Core

Running Get-PSSnapin cmdlet without arguments would let us know what PowerShell Snap-ins are currently loaded.

Now that we know how to start PowerCLI, let’s look at a cmdlets that we will use often:

Get-Help: Get-Help is a command that prints the help for a command argument. By default, if used without any arguments it will print the help for Get-Help.

You can get detailed help on cmdlets by using the following named parameter:

Get-Help Get-Command -full

If you need example of a command line then try this:

Get-Help Get-Alias -Examples

Let’s quickly discuss what is the format for the PowerShell (& also PowerCLI) command line?

A PowerShell command line is made of:

<cmdletName> [cmdletParameters] [cmdletArguments]

The string within angle brackets indicates a required string which is the cmdlet name and the string within the square brackets indicate an optional string which could be named parameters or cmdlet arguments.

A cmdlet is a complex word, made of two parts, the Verb and Noun separated by a hyphen. Where Verb defines the actions, whereas the noun defines on what entity you want to perform the said action. A cmdlet is of the form:

Verb-Noun Arg1 –namedParameter Param1

In the above mentioned command line, the first part is the cmdletName, followed by the second part, the argument to the cmdlet. This is followed with additional modifiers to the cmdlet known as namedParameters. A cmdlet executes in the context of the Argument and the named parameters modify the default behavior of the cmdlet. The named parameters for some commands could be required parameters.

Remember, there could be one or many named parameters and arguments. The named parameters could also be of Boolean type. Where the named parameter acts like a true/false (on/off) switch.

Everything in PowerShell is an Object. A cmdlet (pronounced as command-let) is a command object that you execute in a PowerShell environment. Cmdlets allow you to do some work or execute a task.

Objects have properties and methods. Properties are made of Key/Value pairs. You use the methods to query or modify the properties.

Let’s assume that we want to identify all those cmdlets which are being provided by Say vSphere PowerCLI. Here’s how you can do that:

Rephrasing the question can we ask say, list all cmdlets that are provided by the VMware.VIMAutomation.Core Snapin.

$SnapinName = ‘VMware.VIMAutomation.Core’;
Get-Command | Where-Object { $_.PSSnapin.Name –eq $SnapinName }

Look at the above code:

Line1: Defines a variable and assigns a value, here we are storing the name (a string) of the PowerShell Snapin in variable named $SnapinName. The last semicolon indicates a end of line in PowerShell. The semicolon is optional.

Line2:  Execute Get-Command and pass the output using the pipe. Get-Command prints a collection of cmdlets, functions & aliases (objects). Receive the output from the pipe (|) and filter it using the Where-Object. Where-Object cmdlet processing stops on seeing the closing curly brace. The code between the opening curly brace and the closing curly brace is known as the filter block.

Since we are discussing about the Where-Object, lets take some time to know more about it. Where-Object allows filtering of output based on criteria. Here we print only those cmdlets whose provider Snapin name matches the contents of the variable $SnapinName. Storing the Snapin name within a variable and using it for comparison on Line4 makes the code easy to understand and maintain.

The $_ (dollar-underscore) is a special variable in PowerShell and is a reference in context of the current object.

Consider the following sentence:

This is an Apple. It is red in color.

In the above statement, ‘It’ is a reference in context of the Apple.

Refer to Line2 in the above code block. Here Where-Object received a collection of objects from the pipe and it feeds one object at a time to the filter block. $_ refers to each object from the collection at a time. In this example $_ is referring to each object being sent from the Get-Command cmdlet. So we could have written the code to make it more readable as:

$SnapinName = ‘VMware.VIMAutomation.Core’
Get-Command | Where-Object {
$myCmdlet = $_
$myCmdlet.PSSnapin.Name –eq $sName

Because this a very small piece of code we are not compromising the readability by using the shortcut of not defining the variable on Line2. However In bigger code blocks, it’s a good idea to define a variable instead of using $_ directly.

Now since I wanted to introduce you to some PowerShell code we looked at the above mentioned example. There is another way to find commands that belong to a specific PSSnapin, e.g.

$SnapinName = ‘VMware.VIMAutomation.Core’
Get-Command -Module $SnapinName

Let us say if we want to count the number of cmdlets provided by the said PSSnapin, here’s how we can do that:

$SnapinName = ‘VMware.VIMAutomation.Core’
Get-Command -Module $sName |  Measure-Object -Line

Measure-Object is a cmdlet allows us to count the output. We saw the example for counting lines, but also count for words and characters.

Another useful trick of the Get-Help cmdlet is for finding cmdlets with a specific string in them, say we want to find all the cmdlets which contain the string ‘vmhost’, here how we can find such cmdlets:

help *vmhost*

In the above command we are saying, list all the commands which starts with any string, ends with any string and contains the string ‘vmhost’. Also note that instead of using the cmdlet Get-Help, we are using its alias.

Please note that the Where-Object & Measure-Object are native cmdlet of PowerShell and not provided by PowerCLI. There are other native cmdlets PowerShell cmdlets that we will be using in this guide. Now this is not a PowerShell guide, however we will look at some native cmdlets and discuss them along the way. In addition, I would encourage you to take some time and read through the PowerShell documentation. For your reference I have listed out a few links on the resource list page.


  1. Read the help along with the cmdlet examples for cmdlet Get-Command.
    Hint: to get cmdlet examples use the named parameter -examples along with the Get-Help cmdlet.
  2. Read the help for Measure-Object and count the number words in the help text for Get-Command.
  3. List all those cmdlets which contain the VMHostService string.
  4. List cmdlets that start with the word Stop.
  5. Find the aliases for the cmdlets Get-Command, Measure-Object and Where-Object.
  6. Execute the above exercises using the aliases.

I hope, you have been reading along, keep watching this space for additional articles.

Introduction to PowerCLI

Over the last few years PowerShell has become the default administration and reporting tool in Windows world. Microsoft products such Active Directory, SharePoint, Exchange, SQL & many more already support management via PowerShell cmdlets, making it more popular.

PowerCLI is a Snap-in to PowerShell, which allows you to manipulate and manage all vSphere objects (including VMs, Templates, PortGroups, Datastores and Hosts. One of the reasons why PowerCLI is so popular in the VMware community is because storage vendors such as NetApp & EMC have also released PowerShell Snap-ins to manage their products. Thus knowing PowerShell gets you a lot benefits.

Who would find this guide useful?

This guide shows you a quick way to learn PowerCLI and in the process also learn a few tricks with PowerShell. The guide does not intend to make you a PowerShell or PowerCLI expert. This is a “getting things done” guide for PowerCLI. It’s a kind of a recipe book that shows you how to do things using PowerCLI. If you know the Pareto’s principle, during the course of next few articles we aim to know the important 20% (or maybe 30%) of PowerCLI (and PowerShell) to successfully complete 80% of our day to day administration tasks.

I am no PowerShell or PowerCLI expert; in fact I started PowerShell just a few months back. A lot of this guide takes inspirations from various PowerCLI blogs. Everything that is mentioned in this guide is out there on internet. I have only aggregated that information at one place, hoping that folks find value in it. If I am successful, then this is the guide that I would have liked to have when I started with PowerCLI.

This guide is intended for administrators who use the vSphere Client for their daily administration tasks. And they would like to learn PowerCLI to increase their efficiency and efficacy. The guide assumes you are an administrator well versed with vSphere infrastructure and the vSphere client.

Although any prior experience with PowerShell is not expected, it would surely help if you have some prior shell scripting background. Although this guide is not meant to be learning tool for PowerShell, we would cover some aspects of it so that the topics are relevant for beginners as well.