6

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?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Scott Baker
  • 10,013
  • 17
  • 56
  • 102

5 Answers5

5

Beginning with Windows 10 including Server 2016 and 2019, the Windows Management Framework (which includes the local account management Powershell module) is now shipped "in-box". See https://learn.microsoft.com/en-us/powershell/wmf/overview#wmf-availability-across-windows-operating-systems for information about WMF for other versions of Windows.

This means that functions like Get-LocalUser and New-LocalUser are available without having to install or import any modules.

Note that parameter names have changed for many of these functions from WMF 5.1. Additionally, the password must now be supplied as a secure string which may be generated using the ConvertTo-SecureString function.

Here's an example to check for an existing user and create the user if they don't exit:

$UserName = "AdaLovelace"
$SecurePassword = ConvertTo-SecureString "ThisIsAPassword!" –asplaintext –force
if (Get-LocalUser($UserName))
{
    Echo "User exists"
}
else
{
    New-LocalUser $UserName -Password $SecurePassword -UserMayNotChangePassword -AccountNeverExpires
}
defines
  • 10,229
  • 4
  • 40
  • 56
  • 6
    For anyone coming to this thread late, its important to note that the in-box version only loads from a 64 bit powershell session. – Tim Sep 24 '19 at 18:25
  • @Tim So helpful! Until reading your note I could not figure out why a script containing a call to ```Get-LocalUser``` cmdlet would successfully execute in one account but not in another account. The user in the 'other' account was using a shortcut to 32-bit PS. – BentChainRing Apr 24 '20 at 19:01
  • @Tim, this is one of the best responses. The same 32bit (x86) and 64bit version of powershell also trip me. Issue was in the 32bit , but not there on the 64bit (running on Windows Server 2019) – M.N. Mar 02 '23 at 00:36
2

Microsoft.Powershell.LocalAccounts module comes as part of the Windows Management Framework (WMF) v5.1 that can be downloaded from : https://www.microsoft.com/en-us/download/details.aspx?id=54616

Once installed you'll be able to use those cmdlets in your script. Here you have also a reference of which modules are included with each version of Powershell: https://msdn.microsoft.com/powershell/reference/readme

Good luck! :)

hairowski
  • 81
  • 4
1

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.

BenH
  • 9,766
  • 1
  • 22
  • 35
  • 1
    The first paragraph is not correct. I am using Windows Server 2012 R2 and PS 5.1 - `Get-LocalUser` is working. – Jelphy Jul 03 '18 at 14:13
  • @Jelphy : a module written by a 3rd party isn't the same as an official module (even when it mimics the functions of the original one) – bluuf Jul 03 '18 at 16:49
  • @bluuf I don think Jelphy is referring to Sean Kearney's module. Installing WMF 5.1 does install the official `Microsoft.PowerShell.LocalAccounts` module on down level OS's. I tested on both 2012 R2 and Win7. – BenH Jul 03 '18 at 18:30
0

You need to first grab the .PSM1 file from another computer

#Install LocalAccount Module
Install-Module -Name localaccount

#Save Module to the PowerShell Modules folder
Save-Module -Name localaccount -Path "C:\Program Files (x86)\WindowsPowerShell\Modules"

If you add it to this location on Windows 2008 R2. It should install the module, but if not check out this link: https://msdn.microsoft.com/en-us/library/dd878350(v=vs.85).aspx

I am not sure why it is not on Windows 2008 R2, but the LocalAccounts Module came out in March 21, 2015. This predates Windows 2016 and Windows 10.

Install-Module -Name localaccount -RequiredVersion 1.1

You can always contact the guy who created it "Sean P. Kearney"

Hope this helps you. This is how I did it.

Zach Olinske
  • 517
  • 2
  • 14
0

I was getting this problem because I was trying to use this command in PowerShell (x86). When I changed to 64 bits it solved.

enter image description here

Everything is on here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/get-localuser?view=powershell-5.1

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103