Home > Blockchain >  Set default server in powershell for AD related commands
Set default server in powershell for AD related commands

Time:11-09

Recently my powershell scripts require to explicitly say which domain I want to connect to. Is it necessary to write this for each command? Or can I set it somehow once in the beginning of the script.

Instead of Get-ADUser -Server server otherparameters

could I write in the beginning something like Set-default server to connect to ?

CodePudding user response:

Is it necessary to write this for each command?

No!

You can specify a default parameter value for a parameter belonging to one or more cmdlets by assigning it to the $PSDefaultParameterValues automatic variable:

$PSDefaultParameterValues['*-AD*:Server'] = 'mydc.mydomain.tld'

Any cmdlet you subsequently invoke that matches the *-AD* pattern by name and has a -Server parameter will now implicitly have 'mydc.mydomain.tld' bound to the -Server parameter unless an argument is explicitly passed.

In other words: next time you invoke Get-ADUser rsterba, PowerShell now calls Get-ADUser rsterba -Server 'mydc.mydomain.tld' instead.

For more information about $PSDefaultParameterValues and how it works, see the about_Parameters_Default_Values help topic

  • Related