Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
in PowerShell by 6 7 9

How do I make a parameter required in PowerShell? I have tried to use Mandatory=$true but it's not worked for me and I got "Unexpected attribute parameter", can you guide me on how to set the parameter as mandatory in PowerShell?


1 Answer

1 like 0 dislike
by 155 174 351
selected by
Best answer

To make a mandatory parameter in PowerShell, you should add the "Mandatory=$true" before the parameter definition, not after the parameter name as you did!

Mandatory Parameter in PowerShell


Optional Parameter in PowerShell

     [string]$Company )

For more details, Please check Use PowerShell to Make Mandatory Parameters

If you don’t ask, the answer is always NO!