PowerShell Documentation Question Get-Process

Hi There,

I would like to ask a question that I have not been able to find anywhere.

When we look at the documentation of the Cmdlet Get-Process I see parameters in light blue:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-process?view=powershell-7.3

Get-Process
-Id <Int32> (I can’t format lightblue but here it is lightblue
[-Module]
[-FileVersionInfo]
[]

Another set we see:

Get-Process
-InputObject <Process> (lightblue)
-IncludeUserName (lightblue)
[]

So what does this mean ?

kind regards,

Jason

Hi, welcome to the forum :wave:

The formatting indicates that the parameter is mandatory for that parameter set. Note how the parameters that are not in light-blue are enclosed in square brackets. The square brackets tell you that the parameter is optional.

By the way, your link is broken.

Also, when posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working). If you can’t see the </> in your toolbar, you will find it under the gear icon.

How to format code on PowerShell.org

1 Like

Thank you so much Matt I will definitely adhere to that. Unfortunately I still do not understand it.
If we look at another example:
LinkService

I see that -Name is optional in the the first parameter set :

**Get-Service**
   [[-Name] <String[]>]
   [-DependentServices]
   [-RequiredServices]
   [-Include <String[]>]
   [-Exclude <String[]>]
   [<CommonParameters>]

However the second parameter set :

**Get-Service**
   [-DependentServices]
   [-RequiredServices]
   -DisplayName <String[]>
   [-Include <String[]>]
   [-Exclude <String[]>]
   [<CommonParameters>]

-DisplayName is now seemingly required, but how can this be required when -Name in the first parameter set is optional ?

Also in the third parameter set :

**Get-Service**
   [-DependentServices]
   [-RequiredServices]
   [-Include <String[]>]
   [-Exclude <String[]>]
   [-InputObject <ServiceController[]>]
   [<CommonParameters>]

Why would -InputObject be optional ?

Thanks for the response.

Jason

If you run Get-Service without any parameter it will list all services. If you’re looking for a specific service you can specify its name.

If you’re looking for a specific service but you don’t want to search for it with its name, but with its display name you use the second parameter set and specify its display name. :man_shrugging:t3:

Because you don’t need to provicde some ServiceController objects to it, but you can. If you do so you cannot combine the search for a particular service with its name or display name at the same time.

2 Likes

Hi Olaf,

Thanks a lot for your response. I still don’t understand it. What is the meaning of required then ? I understand what you are saying but with all respect it doesn’t explain why -DisplayName is required and -Name and -InputObject isn’t.

There are 3 ways of retrieving the service (-Name ,-DisplayName, -InputObject ) They should in my way of viewing all be optional since doing a Get-Service on its own doesn’t require a mandatory parameter. Maybe I don’t know what required means in this context and what optional means.

kind regards,

Jason

I actually don’t know how to explain it better. Of course none of the parameters is required. BUT: If you are looking for a particular service and you only know the according display name of the service you are looking for you can specify this display name as the value for the parameter -DisplayName. :man_shrugging:t3:

The parameter name -DisplayName is required when you want to search for a particular service with its display name.

If you’re looking for a particular service with its name you can specify this name and you don’t have to use the parameter name because it is implicitly provided to it.

For example: If you’re looking for the BITS service you can run

Get-Service BITS

and you don’t have to provide the parameter name -Name.

If you want to look for the BITS service with its display name or a part of it you ahve to provide the parameter name -DisplayName

Get-Service -DisplayName 'Background Intelligent*'

If you try to search for the BITS service with its display name without providing the parameter name -DisplayName you will not find it:

Get-Service 'Background Intelligent*'

Its called function or parameter or constructor overloading. Having one function but with different sets of parameters. Very common.
Basic explanation here:
overload wiki
Using it in powershell classes:
powershell overloading

Ok both thanks a lot for your answer but I looked and tested:

actually don’t know how to explain it better. Of course none of the parameters is required. BUT: If you are looking for a particular service and you only know the according display name of the service you are looking for you can specify this display name as the value for the parameter -DisplayName . :man_shrugging:t3:

If I try to retrieve the service spooler on displayname
Get-Service 'print spooler'

This works without providing the parameter -DisplayName

Also if I do :

Get-Service 'print spooler' -RequiredServices

It works

Overloading I am familiar with, you overload a method depending on the arguments you pass.

So maybe I am still not getting it, or people have had the wrong idea about it

Thank you

Jason

I see what you mean. I suspect the doco is wrong. it appears you can supply either the name or display name without specifying the parameter - it will search for both.

I cannot reproduce this behaviour on my system. It just works as documented for me (My Windows is set up in German)

PS C:\> Get-Service Spooler

Status   Name               DisplayName
------   ----               -----------
Running  Spooler            Druckwarteschlange

PS C:\> Get-Service Druckwarteschlange
Get-Service: Cannot find any service with service name 'Druckwarteschlange'.
PS C:\> Get-Service -DisplayName Druckwarteschlange

Status   Name               DisplayName
------   ----               -----------
Running  Spooler            Druckwarteschlange

PS C:\>

Edit:

Very strange … when I try it your way it kind of works your way … but it looks strange because the way I provide the display name the way it will be outputted

PS C:\> Get-Service 'print Spooler'

Status   Name               DisplayName
------   ----               -----------
Running  Spooler            print Spooler

PS C:\> Get-Service 'Print Spooler'

Status   Name               DisplayName
------   ----               -----------
Running  Spooler            Print Spooler

PS C:\> Get-Service 'PRINT Spooler'

Status   Name               DisplayName
------   ----               -----------
Running  Spooler            PRINT Spooler

Interestingly, when you don’t specify -DisplayName it binds the parameter value to Name.

PS E:\Temp> Trace-Command -PSHost -Name ParameterBinding -Expression { Get-Service 'Print Spooler' }
DEBUG: 2023-06-22 09:40:50.4404 ParameterBinding Information: 0 : BIND NAMED cmd line args [Get-Service]
DEBUG: 2023-06-22 09:40:50.4407 ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Get-Service]
DEBUG: 2023-06-22 09:40:50.4409 ParameterBinding Information: 0 :     BIND arg [Print Spooler] to parameter [Name]
DEBUG: 2023-06-22 09:40:50.4410 ParameterBinding Information: 0 :         Binding collection parameter Name: argument type [String], parameter type [System.String[]], collection type Array, element type [System.String], no coerceElementType
DEBUG: 2023-06-22 09:40:50.4411 ParameterBinding Information: 0 :         Creating array with element type [System.String] and 1 elements
DEBUG: 2023-06-22 09:40:50.4412 ParameterBinding Information: 0 :         Argument type String is not IList, treating this as scalar
DEBUG: 2023-06-22 09:40:50.4413 ParameterBinding Information: 0 :         Adding scalar element of type String to array position 0
DEBUG: 2023-06-22 09:40:50.4414 ParameterBinding Information: 0 :         Executing VALIDATION metadata: [System.Management.Automation.ValidateNotNullOrEmptyAttribute]
DEBUG: 2023-06-22 09:40:50.4415 ParameterBinding Information: 0 :         BIND arg [System.String[]] to param [Name] SUCCESSFUL
DEBUG: 2023-06-22 09:40:50.4415 ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Get-Service]
DEBUG: 2023-06-22 09:40:50.4416 ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: 2023-06-22 09:40:50.4417 ParameterBinding Information: 0 : CALLING ProcessRecord
DEBUG: 2023-06-22 09:40:50.4428 ParameterBinding Information: 0 : CALLING EndProcessing

From my limited testing, it looks like providing the service’s display name as the Name parameter always succeeds. Not sure if this is a bug or what the PowerShell team intended.