Explore CmdLets

In this session I will talk about the three important cmdlets which are useful through out your PowerShell journey and will help us to explore various cmdlets and its usage.

What are those CmdLets?

Predominantly the cmdlets below are being used by most of the PowerShell folks, they be a beginner or be an export, they must use these cmdlets to explore various modules and cmdlets through out their PowerShell journey.

Now let’s see what are those cmdlets…

Get-Command

Get-Command cmdlet is used to get all the cmdlets, functions, aliases, applications and etc, which are installed on the computer. However there are bunch of parameters that can be used with Get-Command to filter your output, with the combination of various parameters, you can get all the cmdlets, or you can get only the imported cmdlets, or with some specific name, verb or alias, or even non PowerShell commands as well.

As we discussed earlier, you don’t need to memorize any cmdlets, because Get-Command will help you to get the required cmdlets based on the requirement using the wildcards, for suppose you want to work with ip addresses like to get an ip address or set an ip address and you don’t know which cmdlet that you need use or you forgot the cmdlet, then you can simply use Get-Command using the "*ipaddress*" wildcard to get the required cmdlets and in which you can easily identify the required cmdlet and pick it up for your need, now let’s see how…

Get-Command *ipaddress*

Output:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Get-NetIPAddress                                   1.0.0.0    NetTCPIP
Function        New-NetIPAddress                                   1.0.0.0    NetTCPIP
Function        Remove-NetIPAddress                                1.0.0.0    NetTCPIP
Function        Set-NetIPAddress                                   1.0.0.0    NetTCPIP

As I said earlier, it is easy to identify the cmdlet by looking at the cmdlet name itself and we can judge what that cmdlet does, like Get-NetIPAddress is used to get the ip address or Set-NETIPAddress is used to set the ip address.

I tried to give you a complete set of examples in how to use Get-Command in the reference page, and I request you to go through all the examples and practice thoroughly.

After you identified the cmdlet if you want to know how to use the cmdlet you can refer the cmdlet help on the screen itself by using the Get-Help cmdlet.

Get-Help

PowerShell maintains a help system locally, and Get-Help is used fetch the help of various commands and topics. By default without any other parameter other than the -Name parameter, it will display the basic help, and -Detailed or -Full parameters will add more info on top of the basic help, and -Examples parameter will display all the examples on the cmdlet/function available in the help source, and with -Online parameter it will open the updated/latest help available in the online sources. Now let’s see how to get the basic help of any cmdlet…

Get-Help -Name Get-Disk

# or

Get-Disk -?

Output:

NAME
    Get-Disk
    
SYNOPSIS
    Gets one or more disks visible to the operating system.
    
    
SYNTAX
    Get-Disk [[-Number] <UInt32[]>] [-CimSession <CimSession[]>] [-ThrottleLimit <Int32>] [<CommonParameters>]
    
    Get-Disk [-CimSession <CimSession[]>] [-FriendlyName <String[]>] [-SerialNumber <System.String[]>] [-ThrottleLimit <Int32>] [<CommonParameters>]
    
    Get-Disk [-CimSession <CimSession[]>] [-ThrottleLimit <Int32>] [-VirtualDisk <CimInstance>] [<CommonParameters>]
    
    Get-Disk [-CimSession <CimSession[]>] [-Partition <CimInstance>] [-ThrottleLimit <Int32>] [<CommonParameters>]
    
    Get-Disk [-CimSession <CimSession[]>] [-iSCSISession <CimInstance>] [-ThrottleLimit <Int32>] [<CommonParameters>]
    
    Get-Disk [-CimSession <CimSession[]>] [-iSCSIConnection <CimInstance>] [-ThrottleLimit <Int32>] [<CommonParameters>]
    
    Get-Disk [-CimSession <CimSession[]>] [-Path <String[]>] [-ThrottleLimit <Int32>] [<CommonParameters>]
    
    Get-Disk [-CimSession <CimSession[]>] [-ThrottleLimit <Int32>] [-UniqueId <String[]>] [<CommonParameters>]
    
    Get-Disk [-StorageJob <CimInstance>] [<CommonParameters>]
    
    Get-Disk [-StorageNode <Microsoft.Management.Infrastructure.CimInstance>] [<CommonParameters>]
    
    Get-Disk [-StorageSubSystem <Microsoft.Management.Infrastructure.CimInstance>] [<CommonParameters>]
    
    
DESCRIPTION
    The Get-Disk cmdlet gets one or more Disk objects visible to the operating system, or optionally a filtered list.
    

RELATED LINKS
    Online Version: http://go.microsoft.com/fwlink/?LinkId=812860
    Where-Object 
    Clear-Disk 
    Get-Partition 
    Get-StorageJob 
    Get-StorageSubSystem 
    Initialize-Disk 
    Set-Disk 
    Update-Disk 

REMARKS
    To see the examples, type: "Get-Help Get-Disk -Examples"
    For more information, type: "Get-Help Get-Disk -Detailed"
    For technical information, type: "Get-Help Get-Disk -Full"
    For online help, type: "Get-Help Get-Disk -Online"

You can also use help and man aliases in the place of Get-Help, but it displays the help page by page and one page at a time and even you can use <cmdlet> -? to display the help, but it only works with the cmdlets.

PowerShell has an ability to auto generate the help for your cmdlet/function based on the information available in the cmdlet or the function. PowerShell uses various methods to fetch the information and generate the help, it uses comment based help with in the cmdlet/function, local help files or any online sources to generate the help.

Update-Help will update your local copy of help with the latest help available in the online source. It is ideally good to run in the Update-Help periodically to update your local help.

There are various about topics in PowerShell starts with about_, which will give you detailed understanding of that topic. For example run Get-Help about_Variables to know about the variables in PowerShell, and to see all the help topics run Get-Help about_*.

I have given a complete set of Get-Help examples in the reference page and please practice thoroughly.

Get-Member

Before I actually talk about the Get-Member cmdlet, I should talk about the objects in PowerShell. PowerShell treats everything as an object with reference to a .Net class, in-fact in PowerShell there are no data types rather they are called object types, even so called the int, string, double, float and boolean data types in other languages are called object types in PowerShell. So when you say anything is an object it should have some properties and methods and together called members of the object. Properties represents the object in nature and methods represents the actions. So now using the Get-Member we can get the members of the object, there is a -InputObject parameter to pass the object for which you want to see the members or simply you can pipe that to Get-Member. The members are predominantly categorized as properties and methods.

$Values = Get-Volume
Get-Member -InputObject $Values[0]

# or simply

Get-Volume | Get-Member

Output:

TypeName: Microsoft.Management.Infrastructure.CimInstance#ROOT/Microsoft/Windows/Storage/MSFT_Volume

Name                      MemberType     Definition
----                      ----------     ----------
Clone                     Method         System.Object ICloneable.Clone()
Dispose                   Method         void Dispose(), void IDisposable.Dispose()
Equals                    Method         bool Equals(System.Object obj)
GetCimSessionComputerName Method         string GetCimSessionComputerName()
GetCimSessionInstanceId   Method         guid GetCimSessionInstanceId()
GetHashCode               Method         int GetHashCode()
GetObjectData             Method         void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context), void ISerializable.GetObjectData(Sys...
GetType                   Method         type GetType()
ToString                  Method         string ToString()
AllocationUnitSize        Property       uint32 AllocationUnitSize {get;}
DriveLetter               Property       char DriveLetter {get;}
FileSystem                Property       string FileSystem {get;}
FileSystemLabel           Property       string FileSystemLabel {get;set;}
ObjectId                  Property       string ObjectId {get;}
PassThroughClass          Property       string PassThroughClass {get;}
PassThroughIds            Property       string PassThroughIds {get;}
PassThroughNamespace      Property       string PassThroughNamespace {get;}
PassThroughServer         Property       string PassThroughServer {get;}
Path                      Property       string Path {get;}
PSComputerName            Property       string PSComputerName {get;}
Size                      Property       uint64 Size {get;}
SizeRemaining             Property       uint64 SizeRemaining {get;}
UniqueId                  Property       string UniqueId {get;}
DedupMode                 ScriptProperty System.Object DedupMode {get=switch ($this.psBase.CimInstanceProperties["DedupMode"].Value)...
DriveType                 ScriptProperty System.Object DriveType {get=switch ($this.psBase.CimInstanceProperties["DriveType"].Value)...
FileSystemType            ScriptProperty System.Object FileSystemType {get=switch ($this.psBase.CimInstanceProperties["FileSystemType"].Value)...
HealthStatus              ScriptProperty System.Object HealthStatus {get=switch ($this.psBase.CimInstanceProperties["HealthStatus"].Value)...
OperationalStatus         ScriptProperty System.Object OperationalStatus {get=$_status = @();...

When you pipe any cmdlet to Get-Member it will show members of the output object(s) produced by the actual cmdlet.

There are lot other ways that you can fetch or filter the members of an object.

I have tried my level best to give you the complete set of Get-Member examples in the reference page and please practice thoroughly.


Reference

Thank you, and will see you in the next session