Tuesday 25 August 2015

Parameters in PowerShell

Just Think about how cmdlets know What to do with  Given information.Here comes the Parameters, Which tells cmdlets what to do with given data. Again We need help ... Get-Help.

Do a Get-Help on a cmdlet,Say Get-ChildItem.



PS C:\> Get-Help Get-ChildItem 

NAME
    Get-ChildItem
    
SYNOPSIS
    Gets the items and child items in one or more specified locations.
    
    
SYNTAX
    SYNTAX
    Get-ChildItem [[-Path] <String[]>] [[-Filter] <String>] [-Exclude <String[]>] [-Force] [-Include <String[]>] [-Name] [-Recurse] [-UseTransaction [<SwitchParameter>]] 
    [<CommonParameters>]
    
    Get-ChildItem [[-Filter] <String>] [-Exclude <String[]>] [-Force] [-Include <String[]>] [-Name] [-Recurse] -LiteralPath <String[]> [-UseTransaction 
    [<SwitchParameter>]] [<CommonParameters>]


Here you will get the Help For Get-ChildItem cmdlet and While checking the syntax for this cmdlet, you can see  some Paramaters, which begns with a '-' character

[-Path] <String[]>
The Path Parameter specifies the path to one or more locations,that's why you can see 'String[]' , which represents multiple string inputs.used

[-Filter] <String>
Specifies a Filter for the path parameter,Wildcards can be 

-Include <String[]
As the name,include parameter includes only paths for the strings specified. This parameter accepts Wildcards.

-Exclude <String[]
As the name,exclude parameter omits paths for the strings specified. This parameter accepts Wildcards.If Specified,

-Name <SwitchParameter>
Only names of the items in the locations will be retrieved.

-Recurse <SwitchParameter>
If specified , this will retrieves items in specified Locations and in all child items of the Locations.

-Force <SwitchParameter>
Overrides the restrictions that prevents the cmdlet from executing if Specified.

-LiteralPath <String[]>
Specifies a path to one or more ations, but here it is exactly used as it is typed,In Path parameter, We can use wildcards , which are not allowed here.This cmdlet supports the common parameters:
-Verbose, -Debug, -ErrorAction, -ErrorVariable  and -Outvariable.

Types Of Parameters

  • Named Paramters
  • Positional Parameters
  • Switch Paramters
  • Common Parameters
Named Paramters:- Name parameters Works like  key value pairs. You specify the name of the paarameter which starts with a Hyphen and a space between the value of that parameter.




Positional Parameters:- Positional parameters are parameters that needs to be specified in certain position.For named parameters, position is not having any importance as they are named and can be specified any where, Whereas Positional parameters has to be provided in its own position.

here in Get-Childitem cmdlet, Parameter -Path is having position 1.So it is not mandatory to name the parameter. Similarly Parameter -Filter has the position 2.




Switch Paramters:- If named parameters are like key value pairs, switch parameters are just opposite.Switch parameters represents its presence,like a Physical Switch to Turn Light "On or Off".




Get-ChildItem -Path C:\Windows -Recurse




Here We use -Recurse Switch Parameter to get items fom the subdirectories of "C:\Windows"



Common Parameters:- There are some Commomn Parameters which is common for almost all cmdlets in PowerShell.
  • -Verbose <Switch>
  • -Debug <Switch>
  • -ErrorAction <Value>
  • -ErrorVariable <Value>
  • -OutVariable <Value>





















No comments:

Post a Comment