Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

#23 PowerShell Learning: PowerShell Parameters

August 20, 2023
in Blog, PowerShell Learning
0
ADVERTISEMENT

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
5/5 - (1 vote)
Previous Post

#22 PowerShell Learning: The Philosophy Verb-Noun

Next Post

#24 PowerShell Learning: PowerShell and WMI

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory