Skip to main content

Positional Parameters and Switch Parameters Odd Behavior

So here is a neat one from my class today.  I love it when someone comes in and looks at a problem from a different perspective.  Take a look at these two lines of code:
Get-ChildItem C:\Windows -Recurse

Get-ChildItem -Recurse C:\Windows

They both work!  This is what caught my attention. Look at the same command with all of the parameters named.
Get-ChildItem -Path C:\Windows -Recurse

In the first example, we are using the –Path parameter as positional.  Here is the information form the help file:
PS C:\> Get-Help Get-ChildItem -Parameter Path

-Path <String[]>
    Specifies a path to one or more locations. Wildcards are permitted. The
    default location is the current directory (.).
   
    Required?                    false
    Position?                    1
    Default value                Current directory
    Accept pipeline input?       true (ByValue, ByPropertyName)
    Accept wildcard characters?  true

You can see it is in position 1.  In the second example, we placed in position 2, but it still worked! OK, time to play.  Right off the bat, we suspected that SWITCH parameters do not consume a position so we tested this theory.
Get-ChildItem -Recurse -Directory C:\Windows

It worked.  Both –Recurse and –Directory are SWITCH parameters.  So to play with it further, I decided to add the –Filter parameter which is in position 2.
PS C:\> Get-ChildItem -Path C:\Windows -Filter "System*"


    Directory: C:\Windows


Mode                LastWriteTime         Length Name                         
----                -------------         ------ ----                         
d-----       10/30/2015  12:24 AM                System                       
d-----        4/12/2016   8:54 AM                System32                     
d-----       10/30/2015   2:07 AM                SystemApps                   
d-----       10/30/2015  12:24 AM                SystemResources              
-a----        8/22/2013   6:25 AM            219 system.ini                   


Adding in the switch parameters also worked.
Get-ChildItem -Recurse -Directory -Path C:\Windows -Filter "System*"

Using –Path and –Filter as positional parameters after the switch parameters did not work.
PS C:\> Get-ChildItem -Recurse  -Directory C:\Windows "System*"
Get-ChildItem : Second path fragment must not be a drive or UNC name.
Parameter name: path2
At line:1 char:1
+ Get-ChildItem -Recurse  -Directory C:\Windows "System*"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (C:\:String) [Get-ChildItem], A
   rgumentException
    + FullyQualifiedErrorId : DirArgumentError,Microsoft.PowerShell.Commands.G
   etChildItemCommand

However using them with the switch parameters after the positional parameters do work.
PS C:\> Get-ChildItem C:\Windows "System*" -Recurse  -Directory


    Directory: C:\Windows


Mode                LastWriteTime         Length Name                          
----                -------------         ------ ----                         
d-----       10/30/2015  12:24 AM                System                       
d-----        4/12/2016   8:54 AM                System32                     
d-----       10/30/2015   2:07 AM                SystemApps                   
d-----       10/30/2015  12:24 AM                SystemResources              


When I named one of the positional parameters, it worked.
Get-ChildItem -Recurse  -Directory -Path C:\Windows "System*"

Get-ChildItem -Recurse  -Directory C:\Windows -Filter "System*"

From this quick test run, the best that I can come up with is that when you use switch parameters before positional parameters, only 1 positional parameter will work.  All others need to be named.








Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.