Please note: Because this is a self-answered question, there is no solution attempt in the question itself.
In my PowerShell scripts I'm using the ActiveDirectory module, which contains the various *-AD* cmdlets such as Get-ADUser.
How can I automate (script) the installation of the ActiveDirectory module on Windows 11?
The answer isn't straightforward, for the reasons detailed in the next section:
Note:
- This question is on-topic for this site, because it meets 2 of the 3 criteria (whereas meeting only 1 is sufficient): It is about "software tools commonly used by programmers" and it is also about "a specific programming problem" namely how to perform installation of a specific software tool of interest programmatically.
The ActiveDirectory module does not ship with PowerShell, but - unlike most publicly available modules - its on-demand installation can not be scripted via the usual cmdlets, Install-Module or its successor, Install-PSResource, from their (default) repository, the PowerShell Gallery, because the module isn't available there.
Instead, the module is part of the larger, non-PowerShell-specific set of RSAT tools, which comprise many additional components.
(How these tools must be installed has changed over time; there are many older question relating to previous Windows versions that no longer apply; similarly, questions relating to server versions of Windows do not apply, as the installation method differs; there's no question specifically about scripting the installation that I am a aware of.)
The ActiveDirectory PowerShell module documentation as of this writing only provides an abstract pointer (emphasis added):
Starting with Windows 10 October 2018 Update, RSAT is included as a set of Features on Demand right from Windows 10 [and therefore also in Windows 11]. Now, instead of downloading an RSAT package you can just go to
Manage optional featuresinSettingsand clickAdd a featureto see the list of available RSAT tools. Select and install the specific RSAT tools you need.
- That is, it only mentions a GUI installation and method and even for that it doesn't actually state which tool(s), specifically, you need to select to install the PowerShell module (only).
