Using the Get-Command, Get-Help, and Get-Member cmdlets
There are some PowerShell cmdlets that everyone should know. Knowing these cmdlets will help you to discover other cmdlets, their functions, parameters, and returned objects.
Using Get-Command
The first cmdlet that you should know is Get-Command
. This cmdlet returns all of the commands that are installed on your computer. The Get-Command
cmdlet has the following syntax:
Get-Command [[-ArgumentList] <Object[]>] [-All] [-ListImported] [-Module <String[]>] [-Noun <String[]>] [-ParameterName <String[]>] [-ParameterType <PSTypeName[]>] [-Syntax] [-TotalCount <Int32>] [-Verb <String[]>] [<CommonParameters>] Get-Command [[-Name] <String[]>] [[-ArgumentList] <Object[]>] [-All] [-CommandType <CommandTypes>] [-ListImported] [-Module <String[]>] [-ParameterName <String[]>] [-ParameterType <PSTypeName[]>] [-Syntax] [-TotalCount <Int32>] [<CommonParameters>]
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
The first parameter set is called CmdletSet
and the second parameter set is called AllCommandSet
.
If you type the following command then you will get a list of commands installed on your computer including cmdlets, aliases, functions, workflows, filters, scripts, and applications:
PowerCLI C:\> Get-Command
You can also specify the name of a specific cmdlet to get information about that cmdlet as shown in the following command:
PowerCLI C:\> Get-Command –Name Get-VM
This will return the following information about the Get-VM
cmdlet:
CommandType Name ModuleName ----------- ---- ---------- Cmdlet Get-VM VMware.VimAutomation.Core
You see that the command returns the command type and the name of the module that contains the Get-VM
cmdlet. CommandType
, Name
, and ModuleName
are the properties that the Get-VM
cmdlet returns by default. You will get more properties if you pipe the output to the Format-List
cmdlet:
PowerCLI C:\> Get-Command –Name Get-VM | Format-List * HelpUri : http://www.vmware.com/support/developer/PowerCLI/PowerCLI55R1/html/Get-VM.html DLL : C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\VMware.VimAutomation.ViCore.Cmdlets.dll Verb : Get Noun : VM HelpFile : VMware.VimAutomation.ViCore.Cmdlets.dll-Help.xml PSSnapIn : VMware.VimAutomation.Core ImplementingType : VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM Definition : Get-VM [[-Name] <string[]>] [-Server <VIServer[]>] [-Datastore <StorageResource[]>] [-Location <VIContainer[]>] [-Tag <Tag[]>] [-NoRecursion] [<CommonParameters>] Get-VM [[-Name] <string[]>] [-Server <VIServer[]>] [-DistributedSwitch <DistributedSwitch[]>] [-Tag <Tag[]>] [<CommonParameters>] Get-VM [-Server <VIServer[]>] [-Id <string[]>] [<CommonParameters>] Get-VM -RelatedObject <VmRelatedObjectBase[]> [<CommonParameters>] DefaultParameterSet : Default OutputType : { VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine} Options : ReadOnly Name : Get-VM CommandType : Cmdlet Visibility : Public ModuleName : VMware.VimAutomation.Core Module : RemotingCapability : PowerShell Parameters : {[Name, System.Management.Automation.ParameterMetadata], [Server, System.Management.Automation.ParameterMetadata], [Datastore, System.Management.Automation.ParameterMetadata], [DistributedSwitch, System.Management.Automation.ParameterMetadata]...} ParameterSets : {[[-Name] <string[]>] [-Server <VIServer[]>] [-Datastore <StorageResource[]>] [-Location <VIContainer[]>] [-Tag <Tag[]>] [-NoRecursion] [<CommonParameters>], [[-Name] <string[]>] [-Server <VIServer[]>] [-DistributedSwitch <DistributedSwitch[]>] [-Tag <Tag[]>] [<CommonParameters>], [-Server <VIServer[]>] [-Id <string[]>] [<CommonParameters>], -RelatedObject <VmRelatedObjectBase[]> [<CommonParameters>]}
You can use the Get-Command
cmdlet to search for cmdlets. For example, if you want search for the cmdlets that are used for vSphere hosts, type:
PowerCLI C:\> Get-Command -Name *VMHost*
If you are searching for the cmdlets to work with networks, type:
PowerCLI C:\> Get-Command -Name *network*
Using Get-VICommand
PowerCLI has a cmdlet Get-VICommand
that is similar to the Get-Command
cmdlet. The Get-VICommand
cmdlet is actually a function that creates a filter on the Get-Command
output and returns only PowerCLI commands. Type the following command to list all of the PowerCLI commands:
PowerCLI C:\> Get-VICommand
The Get-VICommand
cmdlet has only one parameter –Name
. So you can also type, for example, the following command to get information only about the Get-VM
cmdlet
PowerCLI C:\> Get-VICommand –Name Get-VM
Using Get-Help
To discover more information about cmdlets, you can use the Get-Help
cmdlet. For example:
PowerCLI C:\> Get-Help Get-VM
will display the following information about the Get-VM
cmdlet:
The Get-Help
cmdlet has some parameters that you can use to get more information. The –examples
parameter shows examples of the cmdlet. The –detailed
parameter adds parameter descriptions and examples to the basic help display. The –full
parameter displays all of the information available about the cmdlet. And the –online
parameter retrieves online help information available about the cmdlet and displays it in a web browser. In PowerShell v3, there is a new Get-Help
parameter –ShowWindow
. This displays the output of Get-Help
in a new window.
The Get-Help Get-Help -ShowWindow
command opens the following screenshot:
Using Get-PowerCLIHelp
The PowerCLI Get-PowerCLIHelp
cmdlet opens a separate help window for PowerCLI cmdlets, PowerCLI objects, and articles. This is a very useful tool if you want to browse through the PowerCLI cmdlets or PowerCLI objects.
Using Get-PowerCLICommunity
If you have a question about PowerCLI and you can't find the answer in this book, use the Get-PowerCLICommunity
cmdlet to open the VMware vSphere PowerCLI section of the VMware VMTN Communities. You can log in to the VMware VMTN Communities using the same My VMware account that you used to download PowerCLI. First, search the community for an answer to your question. If you still can't find the answer, go to the Discussions tab and ask your question by clicking on the Start a discussion button. You might receive an answer to your question in a few minutes.
Using Get-Member
In PowerCLI, you work with objects. Even a string is an object. An object contains properties and methods; these are called members in PowerShell. To see which members an object contains, you can use the Get-Member
cmdlet. To see the members of a string, type:
PowerCLI C:\> "Learning PowerCLI" | Get-Member
Pipe an instance of a PowerCLI object to Get-Member
to retrieve the members of that PowerCLI object. For example, to see the members of a virtual machine object, you can type:
PowerCLI C:\> Get-VM | Get-Member TypeName: VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl Name MemberType Definition ---- ---------- ---------- ConvertToVersion Method T VersionedObjectInterop.Conver... Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() IsConvertableTo Method bool VersionedObjectInterop.IsC... LockUpdates Method void ExtensionData.LockUpdates() ObtainExportLease Method VMware.Vim.ManagedObjectReferen... ToString Method string ToString() UnlockUpdates Method void ExtensionData.UnlockUpdates() CDDrives Property VMware.VimAutomation.ViCore.Typ... Client Property VMware.VimAutomation.ViCore.Int... CustomFields Property System.Collections.Generic.IDic... DatastoreIdList Property string[] DatastoreIdList {get;} Description Property string Description {get;} DrsAutomationLevel Property System.Nullable[VMware.VimAutom... ExtensionData Property System.Object ExtensionData {get;} FloppyDrives Property VMware.VimAutomation.ViCore.Typ... Folder Property VMware.VimAutomation.ViCore.Typ... FolderId Property string FolderId {get;} Guest Property VMware.VimAutomation.ViCore.Typ... HAIsolationResponse Property System.Nullable[VMware.VimAutom... HardDisks Property VMware.VimAutomation.ViCore.Typ... HARestartPriority Property System.Nullable[VMware.VimAutom... Host Property VMware.VimAutomation.ViCore.Typ... HostId Property string HostId {get;} Id Property string Id {get;} MemoryGB Property decimal MemoryGB {get;} MemoryMB Property decimal MemoryMB {get;} Name Property string Name {get;} NetworkAdapters Property VMware.VimAutomation.ViCore.Typ... Notes Property string Notes {get;} NumCpu Property int NumCpu {get;} PersistentId Property string PersistentId {get;} PowerState Property VMware.VimAutomation.ViCore.Typ... ProvisionedSpaceGB Property decimal ProvisionedSpaceGB {get;} ResourcePool Property VMware.VimAutomation.ViCore.Typ... ResourcePoolId Property string ResourcePoolId {get;} Uid Property string Uid {get;} UsbDevices Property VMware.VimAutomation.ViCore.Typ... UsedSpaceGB Property decimal UsedSpaceGB {get;} VApp Property VMware.VimAutomation.ViCore.Typ... Version Property VMware.VimAutomation.ViCore.Typ... VMHost Property VMware.VimAutomation.ViCore.Typ... VMHostId Property string VMHostId {get;} VMResourceConfiguration Property VMware.VimAutomation.ViCore.Typ... VMSwapfilePolicy Property System.Nullable[VMware.VimAutom...
The command returns the full type name of the VirtualMachineImpl
object and all its methods and properties. Remember that the properties are objects themselves. You can also use Get-Member
to get the members of the properties. For example, the following command line will give you the members of the VMGuestImpl
object:
PowerCLI C:\> $VM = Get-VM –Name vCenter PowerCLI C:\> $VM.Guest | Get-Member