Powershell02 – CmdLet Parameters, Variables

Cmdlets can accept parameters which can be used while processing cmdlets. I would like to talk about 2 types of parameters, Default parameters and named parameters

Default parameter: If the parameter is default parameter, you do not have to specify the parameter name, You can use multiple parameters in appropriate position.
§ ProcessName parameter is default parameter in below example, it is not required to use this ParameterName, you can just specify parameter value.

Named parameter: The parameters which needs to be added along with Parameter name are called Named parameters. -ID is named parameter in below example!

PS C:\Users\Vijay> get-process Powershell

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    415      25    83368      96284   613    11.17  10900 powershell

PS C:\Users\Vijay> get-process -Name Powershell

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    389      25    83368      96284   613    11.19  10900 powershell

PS C:\Users\Vijay> get-process -Id 10900

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    417      26    83692      96680   613    11.20  10900 powershell

PS C:\Users\Vijay>

○ Use get-help with -Full option to understand each parameter:
§ Position? Named indicates Named parameter
§ Position? 0 indicates default parameter, and First parameter
Common parameters:
○ There are multiple common parameters which can be applied to app CmdLets. These parameters are developed by Powershell, at times there will be no impact by adding these Common parameters.
§ Documentation (https://technet.microsoft.com/en-us/library/hh847884.aspx or )
○ List of Common parameters
-Debug (db)
-ErrorAction (ea)
-ErrorVariable (ev)
-OutVariable (ov)
-OutBuffer (ob)
-PipelineVariable (pv)
-Verbose (vb)
-WarningAction (wa)
-WarningVariable (wv)
The risk mitigation parameters are:
-WhatIf (wi)
-Confirm (cf)

Introduction to variables:
Similar to all other programming languages, Variables can be used in PowerShell to store values. I will share few examples on using variables.
• User Defined Variables:
○ Variables can be created on the fly to store the results of CmdLets.
§ Ex:

		PS C:\Users\Vijay> $myProcesses = Get-Process
		PS C:\Users\Vijay> $myProcesses.Count
		78
		PS C:\Users\Vijay> 
		
		PS C:\Users\Vijay> $firstTime=Get-Date
		PS C:\Users\Vijay> sleep(5)
		PS C:\Users\Vijay> $nextTime=Get-Date
		PS C:\Users\Vijay> $dif=$nextTime-$firstTime
		PS C:\Users\Vijay> $dif.Seconds
		25
		 
		PS C:\Users\Vijay> #i = 12
		PS C:\Users\Vijay> $i + 23
		23

Environmental variables
○ Similar to environmental variables used in windows (%environmentalVariable%), same can be used in PowerShell using $env:environmentalVariableName.
○ List of all environmental variables available, Example :

	PS C:\Users\Vijay> Get-Childitem env:
	
	PS C:\Users\Vijay> $a = $env:COMPUTERNAME
	PS C:\Users\Vijay> $a
	AIS-VIJAY
	PS C:\Users\Vijay>

Special Variables

Powershell_Special_Characters

Advertisements
This entry was posted in Learn_Powershell, Powershell and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s