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