0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Create User Account Easily

Use the New-LocalUser -Name 'username' -Password (ConvertTo-SecureString 'password' -AsPlainText -Force) command in PowerShell to create a new local user account with a specified password.
📋

Examples

InputNew-LocalUser -Name 'Alice' -Password (ConvertTo-SecureString 'P@ssw0rd' -AsPlainText -Force)
OutputName Enabled Description ---- ------- ----------- Alice True
InputNew-LocalUser -Name 'Bob' -Password (ConvertTo-SecureString '1234' -AsPlainText -Force)
OutputName Enabled Description ---- ------- ----------- Bob True
InputNew-LocalUser -Name 'TestUser' -Password (ConvertTo-SecureString 'Test123!' -AsPlainText -Force)
OutputName Enabled Description ---- ------- ----------- TestUser True
🧠

How to Think About It

To create a user account in PowerShell, you need to specify the username and a secure password. The password must be converted into a secure string format before creating the user. The New-LocalUser cmdlet handles the creation of the account with these details.
📐

Algorithm

1
Get the desired username and password from input.
2
Convert the plain text password into a secure string.
3
Use the <code>New-LocalUser</code> command with the username and secure password.
4
Confirm the user account was created successfully.
💻

Code

powershell
$password = ConvertTo-SecureString 'MyP@ssw0rd' -AsPlainText -Force
New-LocalUser -Name 'NewUser' -Password $password
Write-Output "User 'NewUser' created successfully."
Output
Name Enabled Description ---- ------- ----------- NewUser True User 'NewUser' created successfully.
🔍

Dry Run

Let's trace creating user 'NewUser' with password 'MyP@ssw0rd' through the script

1

Convert password to secure string

Input: 'MyP@ssw0rd' -> Output: SecureString object

2

Create new local user

Command: New-LocalUser -Name 'NewUser' -Password

3

Print confirmation

Output: User 'NewUser' created successfully.

StepActionValue
1Convert passwordMyP@ssw0rd -> SecureString
2Create userName='NewUser', Password=
3Output messageUser 'NewUser' created successfully.
💡

Why This Works

Step 1: Password Conversion

The password must be converted to a SecureString because New-LocalUser requires a secure password format for safety.

Step 2: User Creation

The New-LocalUser cmdlet creates the user account with the given name and secure password.

Step 3: Confirmation Output

A message is printed to confirm the user was created successfully, helping the user know the script worked.

🔄

Alternative Approaches

Using Active Directory Module
powershell
Import-Module ActiveDirectory
New-ADUser -Name 'ADUser' -AccountPassword (ConvertTo-SecureString 'Pass123!' -AsPlainText -Force) -Enabled $true
This method creates a user in Active Directory, suitable for domain environments but requires AD module and permissions.
Using net user command
powershell
net user NewUser MyP@ssw0rd /add
A simple command-line alternative that works on Windows but is less flexible and secure than PowerShell cmdlets.

Complexity: O(1) time, O(1) space

Time Complexity

Creating a single user account is a constant time operation, so it is O(1).

Space Complexity

The script uses a fixed amount of memory for the password and user creation, so space complexity is O(1).

Which Approach is Fastest?

Using New-LocalUser is efficient and secure for local accounts; net user is faster but less secure; Active Directory methods depend on network and permissions.

ApproachTimeSpaceBest For
New-LocalUser cmdletO(1)O(1)Local user creation with security
Active Directory ModuleO(1)O(1)Domain user creation in AD environments
net user commandO(1)O(1)Quick local user creation without PowerShell
💡
Always convert passwords to secure strings before creating users to avoid security risks.
⚠️
Forgetting to convert the password to a secure string causes the user creation to fail.