Book Image

Instant Windows PowerShell Guide

By : Harshul Patel
Book Image

Instant Windows PowerShell Guide

By: Harshul Patel

Overview of this book

Windows PowerShell has become a booming scripting language over the last couple of years. It has extensive support with an ample number of vendor products, providing a standardized platform for automation and administration. It has massive support for all Microsoft products which creates a layer that can easily automate everything. In the latest version, the PowerShell team has introduced much more functionality with thousands of CMDLETs, part of various modules.This book is a quick reference guide to enable you to get the most out of the latest Windows PowerShell techniques. In this book, you will find new enhancements in the latest version of PowerShell with some helpful examples. This book enables you to quickly move from older versions of PowerShell to Version 3.0 and Version 4.0.This practical, example-oriented book helps you to overcome the difficulty of using and discovering CMDLETs by providing precise information about everything that has been newly introduced in the latest version of Windows PowerShell. It also focuses on the new configuration management system with the help of DSC as a new feature of Windows PowerShell v4.0.You will learn how to use the newly introduced CMDLETs and parameters to perform daily routine tasks. You will also learn how to administer the servers remotely and maintain persistent sessions to provide continuity. You will gain an insight into writing efficient scripts by using various parameters, snippets, and workflows to gain more productivity. You will also be introduced to various modules like CimCmdlets, PSScheduledJob, PSDesiredStateConfiguration, and so on in order to enhance your scripts with the latest instrumentation. Finally this book will make you aware of the capabilities of PowerShell v4.0 and how to fully leverage the functionality introduced in the new version.
Table of Contents (7 chapters)

Working with the various parameters of Get-Command (Intermediate)


We will learn about the parameters of Get-Command in different versions of PowerShell.

Getting ready

In PowerShell v2.0, Get-Command only retrieves the CMDLETs available in the present session whereas, in v3.0, it retrieves all the CMDLETs that are installed on the local computer, including modules, functions, workflows, scripts, and so on. It also includes the application in the output available at the $env:Path location.

How to do it...

Execute the following commands:

  1. The following command statements retrieve a list of the workflow CMDLETs and general functions available in the current session:

    PS C :\> Get-Command -Module PSWorkflow
    PS C :\> Get-Command -CommandType Function
    
  2. The following command statement retrieves all the CMDLETs that have the ComputerName parameter:

    PS C :\> Get-Command -ParameterName ComputerName 
    
  3. The following command statement retrieves all the CMDLETs that accept the PSCredential parameter type.

    PS C :\> Get-Command -ParameterType PSCredential
    

How it works...

The following are the newly introduced parameters in PowerShell v3.0 with Get-Command:

  • -All: This parameter helps us retrieve all the CMDLETs, irrespective of conflicting names.

  • -CommandType<CommandTypes>: With this parameter, we can now get the command list by mentioning CommandType explicitly. We can have other CommandTypes, such as ExternalScript, Application, and so on.

  • -ListImported [<SwitchParameter>]: Get-Command, along with the –ListImported parameter, gets the list of CMDLETs available in the current console session. By default, Get-Command retrieves the CMDLETs from all the sessions that are present on the local computer.

  • -ParameterName <String[]>: This parameter helps us retrieve a list of CMDLETs with the specified parameter name in their syntax.

  • -ParameterType <PSTypeName[]>: This parameter helps us retrieve the list of CMDLETs that have the specified parameter type in their syntax.

Tip

For fast typing you can use the # tag to refer to a CMDLET in the command history. For example:

PS C :\> Get-Process
PS C :\> #Get <Tab>

The preceding command statement searches for the Get keyword in the console command history and refers to the matching CMDLET for tab completion. In this case, the Get keyword matches Get-Process and, hence, upon execution, retrieves a list of the running processes.

There's more…

As stated in earlier recipes, Windows PowerShell 4.0 has a new feature called Desired State Configuration (DSC).

Getting the Configuration type CMDLETs

There are a few CMDLETs with the Configuration command types. To retrieve the Configuration command type CMDLETs, run the following command:

PS C :\> Get-Command -CommandType Configuration

Prior to using the preceding command, the DSC feature should be installed in your local server.