epon
epon

Reputation: 21

Powershell - Why some properties have related parameters

I'm in the process of learning PowerShell (v5 to be exact) and I don't seem to follow the logic behind the object properties and parameters.

If we take:

Get-Service | gm

We can see there is a "Name" AliasProperty:

Name                      AliasProperty Name = ServiceName

But we also (confusingly) have a parameter called "-Name" which allows filtering on a given name.

For example:

I can access the name property by doing:

(Get-Service).name

and presumably filter it by piping it.

But I can also do

Get-Service -Name "filter"

My first question would be, is the property related to the parameter? Is the parameter just given as a sort-of related helpful shortcut to filtering on the "name" property?

Secondly, I would like to ask why there isn't a corresponding parameter for every property. For example:

(Get-Service).servicetype

doesn't have a corresponding parameter:

Get-Service -ServiceType

Thanks.

Upvotes: 1

Views: 178

Answers (3)

G42
G42

Reputation: 10019

  1. No. Parameters are arguments accepted by Cmdlets. Properties are things that belong to an object (Input/Output by a Cmdlet)

  2. You can use Where-Object to be more selective based on property values, or Select-Object based on property names.


# Filter based on ServiceType
Get-Service | Where-Object ServiceType -eq Win32ShareProcess

# Filter based on ServiceType... but return only the name
Get-Service | Where-Object ServiceType -eq Win32ShareProcess | Select-Object Name

Upvotes: 2

kdh
kdh

Reputation: 84

Get-Service is a cmdlet to return the services on a computer. It returns object of the type System.ServiceProcess.ServiceController.

This notation:

(Get-Service).Name

Just returns the Name property of whatever object is returned by the command within your parens, and really has nothing to do with the Get-Service command in itself. Any command resulting in one or more objects with a Name parameter would give you output. This could even be different object types like this:

((get-process -name "winlogon"), (Get-Service -name "wuauserv")).Name

winlogon
wuauserv

A cmdlet for working with a particular type of object will often share parameter names with that objects properties. Not all properties from the object will have corresponding parameters though, only the most commonly used.

Is the parameter just given as a sort-of related helpful shortcut to filtering on the "name" property?

More like the other way around. Filtering with the name parameter is the "bona fide" way of getting the services you want.

The cmdlets are there so you don't have to muck around with the properties of .NET objects directly.

Upvotes: 0

4c74356b41
4c74356b41

Reputation: 72171

  1. not really. its just a meaningful way to name a parameter.
  2. why would you want that? you can filter using the select-object cmdlet.

Parameters are mostly named using a logical approach. so if you are looking for a process called notepad you would do

Get-Process -Name notepad

That's the idea (one of) behind Powershell, so its intuitive.
Select-Object example:

Get-Service | Select-Object Name, ServiceType

Upvotes: 0

Related Questions