0
0
PowerShellscripting~5 mins

Parameters in PowerShell - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a parameter in a PowerShell script?
A parameter is a way to pass information into a PowerShell script or function so it can use that information to do its work.
Click to reveal answer
beginner
How do you define a parameter in a PowerShell script?
You define parameters inside a param() block at the start of the script or function.
Click to reveal answer
intermediate
What is the purpose of specifying a parameter type in PowerShell?
Specifying a type helps PowerShell check that the input is the right kind of data, like a number or text, making scripts safer and clearer.
Click to reveal answer
intermediate
How can you make a parameter optional in PowerShell?
You can make a parameter optional by not marking it as mandatory. You can also provide a default value so the script uses that if no input is given.
Click to reveal answer
intermediate
What does the [switch] parameter type do in PowerShell?
A [switch] parameter acts like a simple on/off flag. If you include it when running the script, it is true; if you leave it out, it is false.
Click to reveal answer
Where do you place the param() block in a PowerShell script?
AAt the very start of the script
BAnywhere in the middle of the script
COnly inside functions
DAt the end of the script
What happens if you run a script with a mandatory parameter but do not provide it?
AThe script ignores the parameter
BThe script runs with a default value
CPowerShell asks you to enter the value
DThe script crashes without message
How do you define a parameter with a default value in PowerShell?
A<code>param([string]$name = 'Guest')</code>
B<code>param([string]$name?)</code>
C<code>param([string]$name!)</code>
D<code>param([string]$name*)</code>
What type of parameter is best for a simple on/off option?
A[int]
B[switch]
C[bool]
D[string]
Which of these is a correct way to declare a mandatory parameter?
A<code>[Parameter(Required)] [string]$Name</code>
B<code>[Mandatory] [string]$Name</code>
C<code>[string]$Name = Mandatory</code>
D<code>[Parameter(Mandatory=$true)] [string]$Name</code>
Explain how to create a PowerShell script that accepts a mandatory string parameter and an optional integer parameter with a default value.
Think about using [Parameter(Mandatory=$true)] and assigning a default value with =
You got /3 concepts.
    Describe the difference between a [switch] parameter and a boolean parameter in PowerShell.
    Consider how you use them when calling the script.
    You got /3 concepts.