Default value in Powershell function parameter

gisliScriptingLeave a Comment

Updated : I was asked if there was a use case for this and I added an example on how to create user with some of the default values.

Someone asked me if there was a possibility to have a default value in a function parameter because the code the person wrote was to have if else statement in the code as you can see in this specific code

But better way to set default value for a parameter should be as simple as this, as you can see I have set the type of the parameter to int and the value to 10, so there is no need to type in a default value in a if else statement.

Use case scenario would be like using this for creating users for instance if you would use New-ADUser you can set some of the settings as default. For instance like in this code

Note, this code is incomplete, there is missing password, creating user with out password is not allowed to be enabled. This is only to get an idea

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.