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.
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.
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.
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.
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.
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
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 .
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
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.