Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / How to Create, Change, and Remove Local Users or Groups with PowerShell

June 8, 2023 PowerShellWindows 10Windows 11Windows Server 2019

How to Create, Change, and Remove Local Users or Groups with PowerShell

You can use the built-in PowerShell module, Microsoft.PowerShell.LocalAccounts, to manage local users and groups in Windows. This module allows you to create or delete local users and security groups, and add or remove users from groups. The module is available on Windows Server 2016 and Windows 10 and newer versions. This module is installed with the Windows Management Framework 5.1 in earlier versions of Windows when you upgrade your PowerShell version.

Contents:
  • Create a New Local User with PowerShell
  • Managing Local User Accounts in Windows via PowerShell
  • How to Create and Manage Local Groups Using PowerShell?

There are 15 cmdlets in the LocalAccounts module. You can display the full list of module cmdlets as follows:

Get-Command -Module Microsoft.PowerShell.LocalAccounts

Get-Command Module Microsoft.PowerShell.LocalAccounts

  • Add-LocalGroupMember – add a user to a local security group;
  • Disable-LocalUser – disable a local user account;
  • Enable-LocalUser – enable a local user account;
  • Get-LocalGroup – get information about a local group;
  • Get-LocalGroupMember – view the list of users in a local group;
  • Get-LocalUser – show information about a local user;
  • New-LocalGroup – create a new local group;
  • New-LocalUser – create a local user;
  • Remove-LocalGroup – delete a local group;
  • Remove-LocalGroupMember – remove a member from a local group;
  • Remove-LocalUser – delete a local user;
  • Rename-LocalGroup – rename a local group;
  • Rename-LocalUser – rename a user;
  • Set-LocalGroup – change group settings;
  • Set-LocalUser – change user settings.

Let’s look at some typical tasks for managing local users and groups on a Windows computer by using the PowerShell cmdlets from the LocalAccounts module.

Previously, the Local Users and Groups Management graphical MMC snap-in (lusrmgr.msc), the net user and net localgroup commands were commonly used to manage local users and groups in Windows.

Create a New Local User with PowerShell

Use the New-LocalUser cmdlet to quickly create a new local user account in Windows:

New-LocalUser -Name "TestUser1" -FullName "Test User" -Description "User for tests"

Specify a password for the new user:

new-localuser create with powershell

The length and complexity of the user’s password must be in accordance with local and domain password policies (if the computer is joined to an Active Directory domain).

If you want to use the New-LocalUser cmdlet to automatically create new local users from PowerShell scripts, you can predefine the default user’s password in the script code. The plaintext password must be converted to a secure string:

$pass = ConvertTo-SecureString "WOS_hubP@ss2023!" -AsPlainText -Force
New-LocalUser -Name TestUser2 -Password $password

To add a user to the local Administrators group, run the command:

Add-LocalGroupMember -Group Administrators -Member TestUser2

You can show the list of local users in Windows on the login screen.

You can also use the following options when creating a local Windows user account:

  • -AccountExpires – set the expiration date of the account, after which the account will be automatically deactivated (by default, New-LocalUser creates an account that never expires);
  • -AccountNeverExpires
  • -Disabled – disable an account after creation;
  • -PasswordNeverExpires – set a user’s password to never expire;
  • -UserMayNotChangePassword – the user cannot change the account password.
Use the New-ADUser cmdlet to create a new user in an Active Directory domain.

Managing Local User Accounts in Windows via PowerShell

To list all local Windows users on the current computer, run:

Get-LocalUser

Get-LocalUser: display a list of local accounts

As you can see, there are 6 local accounts on the computer, 4 of which are disabled (Enabled=False), including the built-in Windows Administrator.

To display all the properties of a local account (similar to the Get-ADUser cmdlet that is used to display information about AD domain users), run this command:

Get-LocalUser -Name root | Select-Object *

AccountExpires :
Description :
Enabled : True
FullName :
PasswordChangeableDate : 3/12/2019 10:14:29 PM
PasswordExpires :
UserMayChangePassword : True
PasswordRequired : False
PasswordLastSet : 3/11/2019 10:14:29 PM
LastLogon : 3/11/2019 4:18:17 PM
Name : root
SID : S-1-5-21-2605456602-2293283241-3832290805-1001
PrincipalSource : Local
ObjectClass : User

Look at the PrincipalSource attribute. It contains the type of the user account. It could be:

  • Local Windows user (PrincipalSource: Local)
  • Microsoft accounts (PrincipalSource: Microsoft Account)
  • Azure AD users (PrincipalSource: AzureAD)

To get the value of a specific user attribute, like the last password change date:

Get-LocalUser -Name root | Select-Object PasswordLastSet

Get-LocalUser info from powershell

To change (reset) the user’s password use the command (we suppose that you have already converted the new password to a SecureString):

Set-LocalUser -Name john -Password $UserPassword –Verbose

powershell: create local user (New-LocalUser) ans set password (Set-LocalUser )

You can use Windows Local Administrator Password Solution (LAPS) policy to manage and automatically change local administrator passwords on all computers in a domain.

To set the “Password never expires” flag for the user, use the command:

Set-LocalUser -Name john –PasswordNeverExpires $False

As you can see, you don’t need to convert the UserAccountControl value as when managing user account properties in AD.

Disable the local account:

Disable-LocalUser -Name john

Enable the local user:

Enable-LocalUser -Name john

To remove a local user:

Remove-LocalUser -Name john -Verbose

How to Create and Manage Local Groups Using PowerShell?

You can list the local groups on your Windows device using the command:

Get-LocalGroup

Get-LocalGroup powershell cmdlet

Let’s create a new local group:

New-LocalGroup -Name RemoteSupport -Description 'Remote Support Group'

Now let’s add a couple of local accounts and a group of local administrators to the new group:

Add-LocalGroupMember -Group 'RemoteSupport' -Member ('john','root','Administrators') -Verbose

 create New-LocalGroup and add users Add-LocalGroupMember

Tip. How to create, remove, or add users to the AD domain groups is described in the post Managing Active Directory groups using  PowerShell.

You can also add a user to groups using the following pipeline (in this example, we will add the user to a local group that allows them to access the computer’s desktop remotely over RDP):

Get-Localuser -Name TestUser2 | Add-LocalGroupMember -Group 'Remote Desktop Users'

Display the list of users in the local group:

Get-LocalGroupMember -Group 'RemoteSupport'

If your computer is joined to the AD domain, you can add domain accounts and groups to your local group. Use the following syntax: DomainName\jonhl or DomainName\’domain admins’.

You can add not only local accounts (PrincipalSource – Local), but also domain accounts (Domain), Microsoft accounts (MicrosoftAccount), and Azure accounts (AzureAD) to the local groups.

Get-LocalGroupMember

Use the following syntax to add a Microsoft or AzureAD user to a local group:

Add-LocalGroupMember -Group 'RemoteSupport' -Member ('MicrosoftAccount\[email protected]','AzureAD\[email protected]') –Verbose

To list the local groups that a specific user is a member of, run the following script (the script checks membership for each local group):


$user='john'
foreach ($LocalGroup in Get-LocalGroup)
{
if (Get-LocalGroupMember $LocalGroup -Member $user –ErrorAction SilentlyContinue)
{
$LocalGroup.Name
}
}

To remove a user from a group, execute the command:

Remove-LocalGroupMember -Group 'RemoteSupport' –Member john

To manage local users on a remote computer, you can connect to the computer through WinRM by using the Invoke-Command or Enter-PSSession cmdlets.

For example, you might want to get a list of accounts in the local group on remote computers:

$winrm_ssn = new-pssession -computer Lon-Srv01,Lon-Srv02,Lon-Srv03
invoke-command -scriptblock {Get-LocalGroupMember -Group 'RemoteSupport'} -session $winrm_ssn -hidecomputername | select * -exclude RunspaceID | out-gridview -title "LocalAdmins"

0 comment
2
Facebook Twitter Google + Pinterest
previous post
Fix: BSOD Error 0x0000007B (INACCESSABLE_BOOT_DEVICE) on Windows
next post
Allow Non-admin Users RDP Access to Windows Server

Related Reading

How to Connect VPN Before Windows Logon

November 14, 2023

Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

November 7, 2023

Send Emails with Microsoft Graph API and PowerShell

November 6, 2023

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • How to Connect VPN Before Windows Logon

    November 14, 2023
  • Removing Azure Arc Setup Feature on Windows Server 2022

    November 9, 2023
  • Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

    November 7, 2023
  • Send Emails with Microsoft Graph API and PowerShell

    November 6, 2023
  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Fix: Remote Desktop Licensing Mode is not Configured
  • Configuring Port Forwarding in Windows
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • How to Install Remote Server Administration Tools (RSAT) on Windows
  • How to Delete Old User Profiles in Windows
  • Adding Drivers into VMWare ESXi Installation Image
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top