PowerShell Question Time: Whats the deal with Positional Parameters?

Hi guys,

Recently a colleague reached out to me with a question about PowerShell that I thought might benefit others.  I thought I’d share it with you here.

Hi Stephen,

Get-ChildItem -filter *.exe -Recure  -path c:\windows
Get-ChildItem -path c:\windows -filter *.exe -recurse

-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

I’m confused in the two commands displayed at the top which both return the same value, how do they both work. If -path requires position 1 and in the first command its all the way at the end how can it be acceptable, or am I not seeing the information correctly. Perhaps this reads

Hopefully that made sense and hopefully I am assuming correctly.

Thanks.

Good question!  Well, first and foremost let me begin by saying that you should never use positional parameters, so just forget about them and move on!

No, you’re still here?  You clicked past the ‘More’ button?  Okay, fine I’ll give a better explanation.

Simply put positional parameters only matter when you leave out the parameter name.

The reason you’re getting the same result, is that you’re explicitly specifying the parameter when you name it with its full or abbreviated name.   An argument is simply only assigned to a particular parameter by its position if you omit the parameter name.

That’s why you can run :

Get-ChildItem C:\windows *.exe  

but you can’t run

Get-ChildItem *.exe c:\windows.  

As the Positional parameter for position one for the command Get-ChildItem is ‘Path’, while Positional Parameter two is reserved for filtering operations.

So, don’t get caught up on positional parameters, using them is EXPRESSLY a bad PowerShell practice for code simplicity and readability sake. You can always put all of your parameters in whichever order you want, and you should always write out your parameter names in entirety, which means that Positional Parameters aren’t helpful whatsoever.

Advertisements

One thought on “PowerShell Question Time: Whats the deal with Positional Parameters?

  1. Alex December 13, 2015 / 10:00 pm

    “Get-ChildItem -filter *.exe -Recure -path c:\windows”

    That command actually won’t do the same thing as the other, because there is a typo in recurse.

Have a code issue? Share your code by going to Gist.github.com and pasting your code there, then post the link here!

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