Overview

I was documenting some processes which involved a number of the Powershell Active Directory cmdlets, the Get-AD* family.

The systems I was working on were not configured in such a way that the AD cmdlets could determine the Active Directory Domain Services instance to connect to.

As a result each Get-AD* call required that the -Server <domain> parameter was explicitly defined. This is fine, preferable even, for documentation purposes to make it clear where a parameter is required and the value being used.

However for day-to-day use I wondered if there was some mechanism to define defaults for these parameters, when the expected values are known and relatively static. Such that they do not need to be explicitly added each time.

Powershell provides Parameter Default Values via the $PSDefaultParameterValues preference variable, which was exactly what I was looking for.

Examples

On a system where the -Server defaults can be successfully discovered by the cmdlet, you can simply run:

PS C:\Users\vagrant> Get-ADUser dallas | Select-Object Name, UserPrincipalName

Name   UserPrincipalName
----   -----------------
dallas dallas@nostromo.com

On a system where the -Server defaults cannot be successfully discovered, the following behaviour will be seen:

PS C:\Users\vagrant> Get-ADUser dallas | Select-Object Name, UserPrincipalName

Get-ADUser : Unable to find a default server with Active Directory Web Services running.
At line:1 char:1
+ Get-ADUser dallas
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (dallas:ADUser) [Get-ADUser], ADServerDownException
    + FullyQualifiedErrorId : ActiveDirectoryServer:1355,Microsoft.ActiveDirectory.Management.Commands.GetADUser

This can be overcome by specifying a specific, resolvable -Server, for example:

PS C:\Users\vagrant> Get-ADUser -Server WIN.AD parker | Select-Object Name, UserPrincipalName

Name   UserPrincipalName
----   -----------------
parker parker@nostromo.com

We can define a standard -Server response to apply each time we run Get-ADUser by setting the following:

$PSDefaultParameterValues['Get-ADUser:Server'] = WIN.AD

On Windows systems, Powershell will run the ps1 script (if one exists) at the value defined in $PROFILE.CurrentUserCurrentHost. On a normal Windows system this would be Microsoft.PowerShell_Profile.ps1 under C:\Users\<username>\Documents\WindowsPowerShell\ where they will be sourced into the shell on startup.

Multiple entries may be defined to cover a range of cmdlets:

$PSDefaultParameterValues['Get-ADComputer:Server'] = WIN.AD
$PSDefaultParameterValues['Get-ADGroup:Server'] = WIN.AD
$PSDefaultParameterValues['Get-ADGroupMember:Server'] = WIN.AD
$PSDefaultParameterValues['Get-ADUser:Server'] = WIN.AD

Or a wildcard can be used to reduce the number of entries (at the expense of clarity):

$PSDefaultParameterValues['Get-AD*:Server'] = WIN.AD

Summary

Powershell Parameter Default Values provides a mechanism to define a standard paramater reponse persistently in the environment for any cmdlets, not just the Get-/Set-AD* family.

Further reading