How to startup PowerShell scripting

For the major use case the PowerShell is designed for systems administrators and engineers.  The PowerShell framework has rich features that helps self learning about its usage.  Further to that its cmdlet syntax using verb-noun helps you easily find the relevant cmdlets.

 

Here is how you get to know/find something within PowerShell

  1. Use Get-Command to see if there is any cmdlet that is already available for doing the action you are looking for.

    PS C:> Get-Command *drive

    CommandType Name ModuleName
    ———– —- ———-
    Cmdlet Get-PSDrive Microsoft.PowerShell.Management
    Cmdlet New-PSDrive Microsoft.PowerShell.Management
    Cmdlet Remove-PSDrive Microsoft.PowerShell.Management

    PS C:>

  2. Getting quick more details about the cmdlets matching your search.

    PS C:> Get-Help *drive

    Name Category Module Synopsis
    —- ——– —— ——–
    Get-PSDrive Cmdlet Microsoft.PowerShell.M… Gets drives in the current session.
    New-PSDrive Cmdlet Microsoft.PowerShell.M… Creates temporary and persistent mapped networ…
    Remove-PSDrive Cmdlet Microsoft.PowerShell.M… Deletes temporary Windows PowerShell drives an…

    PS C:>

  3. Knowing more details about the particular cmdlet that you are interested in 

    PS C:> Get-Help Remove-PSDrive

    NAME
    Remove-PSDrive

    SYNOPSIS
    Deletes temporary Windows PowerShell drives and disconnects mapped network drives.

    SYNTAX
    Remove-PSDrive [-Name] <String[]> [-Force [<SwitchParameter>]] [-PSProvider <String[]>] [-Scope <String>]

    DESCRIPTION
    The Remove-PSDrive cmdlet deletes temporary Windows PowerShell drives that were created by using the New-PSDrive
    cmdlet.

    RELATED LINKS
    Online Version:
    http://go.microsoft.com/fwlink/?LinkID=113376
    Get-PSDrive
    New-PSDrive
    about_Providers

    REMARKS
    To see the examples, type: "get-help Remove-PSDrive -examples".
    For more information, type: "get-help Remove-PSDrive -detailed".
    For technical information, type: "get-help Remove-PSDrive -full".
    For online help, type: "get-help Remove-PSDrive -online"

    PS C:>

  4. Find how to run a chosen cmdlet with valid parameters.                                              Use: get-help Remove-PSDrive -examples
  5. Finding whole help documentation of a cmdlet.                                                            Use: get-help Remove-PSDrive –full
  6.  

Understanding PowerShell Runtime:

As everything in PowerShell is/can be an object, the runtime of PowerShell consists of object and it’s methods/properties execution.

 

Usually in many scripting/programming languages the very common issue the authors encounter is the object type mismatch while assigning and/or matching the results of a command/call. 

In PowerShell you can easily get to know what kind of objects you are getting from a command/function as result. 

  1. Use Get-Member will shows what type of an object returned from a cmdlet

    PS C:> Get-Help | Get-Member | select -first 1

       TypeName: System.String

    Name  MemberType Definition
    —-  ———- ———-
    Clone Method     System.Object Clone()

    PS C:>

  2. Further to that it also gives you find what all methods available with that object, what are the properties that you can query/set for

    PS C:> Get-Help | Get-Member | Group MemberType

    Count Name                      Group
    —– —-                      —–
       33 Method                    {System.Object Clone(), int CompareTo(System.Object value), int CompareTo(string strB), bool Contains(string value), Sys…
        6 NoteProperty              {System.String Category=HelpFile, System.String Component=, System.String Functionality=, System.String Name=default…}
        1 ParameterizedProperty     {char Chars(int index) {get;}}
        1 Property                  {System.Int32 Length {get;}}

    PS C:>

  3. Its possible that a cmd output be a simple string being just one object or an string array with multiple objects.  To know the output is a simple string or string array, use below measure-object cmdlet. If the count is 1, then it’s a simple string otherwise a string array
  4. PS C:> "PowerShell", "cSharp", "Dotnet" | Measure-Object

    Count    : 3

    PS C:> "PowerShell" | Measure-Object

    Count    : 1

    PS C:>

  5. g

Leave a Reply

Your email address will not be published. Required fields are marked *