Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Windows PowerShell Guide

You're reading from   Instant Windows PowerShell Guide Enhance your knowledge of Windows PowerShell and get to grips with its latest features

Arrow left icon
Product type Paperback
Published in Nov 2013
Publisher Packt
ISBN-13 9781849686785
Length 86 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Harshul Patel Harshul Patel
Author Profile Icon Harshul Patel
Harshul Patel
Arrow right icon
View More author details
Toc

Setting default parameter values (Intermediate)


We will learn to set the default parameter values.

Getting ready

$PSDefaultParameterValues is a default preference variable introduced in PowerShell v3.0. This variable sets the custom parameter values to specified CMDLETs and functions. It maintains a hash table to store all of the defined values.

How to do it...

Try the following test code to set the default parameter values:

  1. The following command statement sets the localhost value to the parameter name ComputerName for all CMDLETs that have Invoke as a verb:

    PS C :\> $PSDefaultParameterValues=@{"Invoke-*:ComputerName"="localhost"} 
    

    Tip

    $PSDefaultParameterValues accepts wildcard characters. Also, we can use the ; (semicolon) character to assign multiple parameter values to the $PSDefaultParameterValues variable.

  2. The following command provides the computer name, specified as C:\Servers.txt, to the ComputerName parameter for all CMDLETs that have Invoke as a verb:

    $PSDefaultParameterValues=@{"Invoke-*:ComputerName"={Get-Content C:\Servers.txt} }
    

How it works...

The following are the syntaxes to define the $PSDefaultParameterValues preference variable:

  • $PSDefaultParameterValues=@{"<CmdletName>:<ParameterName>"="<DefaultValue>"}: This syntax maps default values to the combination of the specified CMDLET and ParameterName.

  • $PSDefaultParameterValues=@{"<CmdletName>:<ParameterName>"={<ScriptBlock>}}: This syntax provides the facility to pass ScriptBlock as the default value to the combination of the specified CMDLET and ParameterName.

  • $PSDefaultParameterValues["Disabled"]=$true | $false: This syntax enables/disables the behavior of the $PSDefaultParameterValues variable.

  • $PSDefaultParameterValues[Disabled]=$true: This command statement maintains the list of values in $PSDefaultParameterValues, but it disables its behavior in the current session. You can reset its behavior by using the following syntax: $PSDefaultParameterValues[Disabled]=$false. There is also an alternative to control the behavior of $PSDefaultParameterValues.

    • To disable the behavior, use $PSDefaultParameterValues.Add("Disabled", $true).

    • To enable the behavior use $PSDefaultParameterValues.Remove("Disabled").

      The previous value of this variable will again be in effect in the current PowerShell console.

There's more…

The following are a couple of functionality changes with respect to Windows PowerShell v4.0.

PipelineVariable – a new common parameter

This common parameter allows us to store the current pipeline object in the specified variable. This technique is very useful when dealing with multiple commands in a pipeline with transformative information. In such cases, we will sometimes lose context and be unable to retrieve the data as and when required. We can use the pipeline variable to save the result and that can be passed through the remainder of the pipeline.

For example, in the case of System Center Orchestrator, this parameter helps to extend the context of iterative pipelines.

Collection filtering using method syntax

With the beginning of Windows PowerShell v4.0, we can now filter a collection of objects using a simplified where syntax when a method calls.

PS C :\> (Get-Command).where("Name -like *log")

The preceding command statement retrieves all the CMDLETs ending with the log keyword.

Prior to using this, we need to import the PSDesiredStateConfiguration module as collection filtering is a part of it.

PS C :\> Import-Module PSDesiredStateConfiguration
PS C :\> (Get-Command).where
Script              : $prop, $psop, $val = [string] $args[0] -split
                      '(-eq|-ne|-gt|-ge|-lt|-le|-like|-notlike|-match|-notmatch)'
                      $operation = @{ Prop = $prop.Trim(); Value = $val.Trim(); $psop = $true }
                      $this | where @operation
OverloadDefinitions : {System.Object where();}
MemberType          : ScriptMethod
TypeNameOfValue     : System.Object
Value               : System.Object where();
Name                : where
IsInstance          : False

The where() method is not limited to PowerShell 4.0 only. The following syntax can be used to enable this on systems with PowerShell v3.0 as well:

PS C :\> Update-TypeData -Force -MemberType ScriptMethod -MemberName where -TypeName System.Array -Value { $prop, $psop, $val = [string] $args[0] -split '(-eq|-ne|-gt|-ge|-lt|-le|-like|-notlike|-match|-notmatch)' $operation = @{ Prop = $prop.Trim(); Value = $val.Trim(); $psop = $true } $this | where @operation}

Run the preceding command statement, which enables the where method signature into your PowerShell 3.0 console, and execute commands as follows:

PS C :\> (Get-Command).where("Name -like *log")

This solution can be leveraged if you don't want to import the PSDesireStateConfiguration module explicitly.

You have been reading a chapter from
Instant Windows PowerShell Guide
Published in: Nov 2013
Publisher: Packt
ISBN-13: 9781849686785
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image