Search code examples
powershellmodulewindows-server-2008-r2

Can't find Microsoft.Powershell.LocalAccounts module (or run Get-LocalUser)


When running a script, I have a line to verify that a "service account" (aka a local user account) for our app exists:

$svcAccountName = "TheAccountName"
$svcAccount = Get-LocalUser -Name $svcAccountName

The server (Windows Server 2008 R2) is balking at the Get-LocalUser cmdlet, stating:

Get-LocalUser : The term 'Get-LocalUser' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Get-LocalUser 
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-LocalUser:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

So I tried to import the LocalAccounts module:

Import-Module Microsoft.Powershell.LocalAccounts

and I got this:

Import-Module : The specified module 'LocalAccounts' was not loaded because no
valid module file was found in any module directory.
At line:1 char:1
+ Import-Module
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (LocalAccounts:String)[Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

The server is running PSVersion 4.0 according to the $PSVersionTable variable.

Why isn't the LocalAccounts module loaded and the Get-LocalUser command running? How can I fix this?


Solution

  • This cmdlet was made available with Server 2016 and Win10 1607+. On earlier OS's, you will either need to use net.exe, WMI, ADSI, or a module that uses one of those methods or install WMF 5.1.

    Edit: The MS PFE Sean Kearney written made a module called localaccount. Which is built from the code of this GitHub repostiory which replicates the functionality of the new modules via ADSI. Which works with older versions of PowerShell. Note that this is not the same as the builtin in module.