#23 PowerShell Learning: PowerShell Parameters

Table of Contents

Fundamentals of Parameters

Mandatory Parameters (Required)

How do I know that this parameter is a named and mandatory parameter? I’m good in reading the help. Type

Get-Help Stop-Process -Parameter Name

In the first time when you ran the Get-Help command. PowerShell requires you to update the help database. Type Y then hit enter to update the database.

PS C:\> Get-Help Stop-Process -Parameter Name

Do you want to run Update-Help?
The Update-Help cmdlet downloads the most current Help files for Windows PowerShell modules, and installs them on your computer. For
more information about the Update-Help cmdlet, see https:/go.microsoft.com/fwlink/?LinkId=210614.
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y

As we can see, this parameter is required and named. What does this actually mean?

  • Required: means that this parameter is mandatory. So, you have to specify the name of the process.
  • Named: signifies that you have to type the parameter name and the argument. Everything else leads to an error message.
PS C:\> Get-Help Stop-Process -Parameter Name

-Name <System.String[]>
    Specifies the process names of the processes to stop. You can type multiple process names, 
    separated by commas, or use wildcardcharacters.

    Required?                    true
    Position?                    named
    Default value                None
    Accept pipeline input?       True (ByPropertyName)
    Accept wildcard characters?  true

Positional Parameters

Which brings me to the next example. Most of PowerShell parameters are not mandatory, nor named. Some of them are positional parameters. Let’s take a look to this second example that shows the parameter LogName of the Get-EventLog cmdlet.

PS C:\> Get-Help Get-Eventlog -Parameter LogName

-LogName <string>

    Required?                    true
    Position?                    0
    Accept pipeline input?       false
    Parameter set name           LogName
    Aliases                      LN
    Dynamic?                     false

Crazy thing. It’s required but it’s position is not named, but 0. This means that you have to specify the LogName, but you can do it without the parameter name. The first argument that is given will be automatically bound to this parameter.. Let’s give it a try. PowerShell is asking me for the LogName, because LogName is a mandatory parameter. (Required? true)

PS C:\> Get-EventLog

cmdlet Get-EventLog at command pipeline position 1
Supply values for the following parameters:
LogName:

There are 3 main types of Windows Event Logs: System, Application and Security. What happens if I enter one of the three without the parameter name? It works because the parameter is not named.

PS C:\> Get-EventLog System | select -First 5

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
  430353 Aug 20 10:38  Warning     Netwtw10               2147489710 6062 - Lso was triggered
  430352 Aug 20 10:38  Information Netwtw10               1073748827 7003 - Roam Complete
  430351 Aug 20 10:38  Information Netwtw10               1073748845 7021 - Connection telemetry fields and analysis...
  430350 Aug 20 09:50  Information Microsoft-Windows...           12 Process C:\Windows\System32\WUDFHost.exe (proce...
  430349 Aug 20 09:50  Information Microsoft-Windows...          566 The description for Event ID '566' in Source 'M...

Switched Parameters

What if a Parameter does not require an argument? I mean parameters without any input, but with default values. They are called switched parameters. Let’s say you want to show what will happen if you restart your computer. (We’ve already done this before). The parameter for this is WhatIf.

PS C:\> Get-Help Restart-Computer -Parameter WhatIf

-WhatIf

    Required?                    false
    Position?                    Named
    Accept pipeline input?       false
    Parameter set name           (All)
    Aliases                      wi
    Dynamic?                     false

PS C:\> Get-Help Restart-Computer -Parameter ComputerName

-ComputerName <string[]>

    Required?                    false
    Position?                    0
    Accept pipeline input?       true (ByValue, ByPropertyName)
    Parameter set name           (All)
    Aliases                      CN, __SERVER, Server, IPAddress
    Dynamic?                     false

Now let’s compare the WhatIf parameter with another parameter which calls for an argument, for example a string. Note the highlight. Unlike WhatIf, ComputerName requires a string.

WhatIf requires no input. ComputerName calls for an HostName. This means that you can run the WhatIf parameter without any input. Look at this:

PS C:\> Restart-Computer -WhatIf
What if: Performing operation "Enable the Local shutdown access rights and restart the computer." 
on target "localhost (DESKTOP-B2TOHJT)".

But we encounter problems when we try to restart a remote computer without specifying it’s name.

PS C:\> Restart-Computer -ComputerName
Restart-Computer : Missing an argument for parameter 'ComputerName'. 
Specify a parameter of type 'System.String[]' andtry again.
At line:1 char:18
+ Restart-Computer -ComputerName
+                  ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Restart-Computer], ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.RestartComputerCommand

It’s not possible to cover all topics of parameters in a single blog post. You can start exploring all parameters by using the following command:

PS C:\> Get-Help Get-Eventlog -Parameter *

-After <datetime>

    Required?                    false
    Position?                    Named
    Accept pipeline input?       false
    Parameter set name           LogName
    Aliases                      None
    Dynamic?                     false


-AsBaseObject

    Required?                    false
    Position?                    Named
    Accept pipeline input?       false
    Parameter set name           LogName
    Aliases                      None
    Dynamic?                     false


-AsString

    Required?                    false
    Position?                    Named
    Accept pipeline input?       false
    Parameter set name           List
    Aliases                      None
    Dynamic?                     false


-Before <datetime>

    Required?                    false
    Position?                    Named
    Accept pipeline input?       false
    Parameter set name           LogName
    Aliases                      None
    Dynamic?                     false


-ComputerName <string[]>

    Required?                    false
    Position?                    Named
    Accept pipeline input?       false
    Parameter set name           (All)
    Aliases                      Cn
    Dynamic?                     false


-EntryType <string[]>

    Required?                    false
    Position?                    Named
    Accept pipeline input?       false
    Parameter set name           LogName
    Aliases                      ET
    Dynamic?                     false


-Index <int[]>

    Required?                    false
    Position?                    Named
    Accept pipeline input?       false
    Parameter set name           LogName
    Aliases                      None
    Dynamic?                     false


-InstanceId <long[]>

    Required?                    false
    Position?                    1
    Accept pipeline input?       false
    Parameter set name           LogName
    Aliases                      None
    Dynamic?                     false


-List

    Required?                    false
    Position?                    Named
    Accept pipeline input?       false
    Parameter set name           List
    Aliases                      None
    Dynamic?                     false


-LogName <string>

    Required?                    true
    Position?                    0
    Accept pipeline input?       false
    Parameter set name           LogName
    Aliases                      LN
    Dynamic?                     false


-Message <string>

    Required?                    false
    Position?                    Named
    Accept pipeline input?       false
    Parameter set name           LogName
    Aliases                      MSG
    Dynamic?                     false


-Newest <int>

    Required?                    false
    Position?                    Named
    Accept pipeline input?       false
    Parameter set name           LogName
    Aliases                      None
    Dynamic?                     false


-Source <string[]>

    Required?                    false
    Position?                    Named
    Accept pipeline input?       false
    Parameter set name           LogName
    Aliases                      ABO
    Dynamic?                     false


-UserName <string[]>

    Required?                    false
    Position?                    Named
    Accept pipeline input?       false
    Parameter set name           LogName
    Aliases                      None
    Dynamic?                     false

Some useful parameter examples to play with

At the end I would like to provide some useful examples you can play with. Don’t worry if the commands seems very complex to you. It’s ok. We haven’t done some things yet. They’re just for playing.

Retrieve the newest 5 Event Log Errors from the System Log:

PS C:\> Get-EventLog -LogName System -Newest 5 -EntryType Error | Format-Table Time,EntryType,Message -AutoSize -Wrap

Time EntryType Message
---- --------- -------
         Error The mutual authentication between the local Bluetooth adapter and a device with Bluetooth adapter
               address (b4:ee:25:89:6d:23) failed.
         Error Miniport Microsoft Wi-Fi Direct Virtual Adapter #2, {6a0b0f63-385e-4dd0-be0f-b0bcc0314f87}, had event 74
         Error Miniport Microsoft Wi-Fi Direct Virtual Adapter #2, {6a0b0f63-385e-4dd0-be0f-b0bcc0314f87}, had event 74
         Error Miniport Microsoft Wi-Fi Direct Virtual Adapter #2, {6a0b0f63-385e-4dd0-be0f-b0bcc0314f87}, had event 74
         Error Miniport Microsoft Wi-Fi Direct Virtual Adapter #2, {6a0b0f63-385e-4dd0-be0f-b0bcc0314f87}, had event 74

Get the Filename of a Windows Process

PS C:\> Get-Process notepad -FileVersionInfo

ProductVersion   FileVersion      FileName
--------------   -----------      --------
11.2306.15.0     11.2306.15.0     C:\Program Files\WindowsApps\Microsoft.WindowsNotepad_11.2306.15.0_x64__8wekyb3d8b

Show all *.txt files in C:Temp

Get-ChildItem -Filter *.txt -Path C:\temp
#Learning PowerShell (Articles in series)Cmdlets
#1Getting Started with Windows PowerShell
#2Working with Windows PowerShell
#3Writing and Running PowerShell Scripts
#4Windows PowerShell Remoting
#5Windows PowerShell Profiles
#6PowerShell Drives and Providers
#7Windows PowerShell ISE
#8Manage Services using PowerShell
#9Manage Processes using PowerShellGet-Member, -Property, -ExpandProperty
#10Variables in Windows PowerShell
#11Arrays in Windows PowerShell
#12Hash Tables in Windows PowerShell
#13PowerShell Loopsforeach, for
#14Conditional Logic in PowerShell
#15PowerShell Functions
#16One-liners and the Pipeline
#17Everything about PSCustomObject
#18Selecting Parts of Objects
#19Sorting Objects
#20Getting WMI objects with Get-CimInstance
#21Parentheses, Braces and Square Brackets() {} []
#22The Philosophy Verb-NounGet-Command, Verb-Noun
#23PowerShell Parameters$
#24PowerShell and WMIGet-CimInstance

Leave a Comment

Required fields are marked *