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

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.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the ma...