Latest posts by Dan Franciscus (see all)
- SolarWinds Server Performance and Configuration Bundle - Tue, Jun 18 2019
- SolarWinds Patch Manager: Updating Windows and third-party software - Tue, Apr 30 2019
- Monitor file changes in Windows with PowerShell and pswatch - Fri, Feb 1 2019
With the general release of PowerShell Core 6.0, users now have the ability to use the previously strictly Windows-based scripting language on Linux and macOS. This is quite a great achievement from the PowerShell team. It not only enables IT pros to manage Windows from other operating systems but also lets them use PowerShell for Linux and macOS IT administration tasks.
Here, I am going to show how you can create Linux users with PowerShell. The idea was to try to port the Windows PowerShell cmdlet New-LocalUser to Linux. Now obviously, Linux creates local users differently than Windows, but the idea is essentially similar.
How does Linux create local users? ^
Before we write our function, let's look at the command that creates Linux users: useradd. When comparing useradd with New-LocalUser, I attempted to find similar parameters I could use. Below is a table of these:
|-Name||No named parameter, but you can specify one after useradd|
You could certainly use other parameters with useradd, but I will work with these to make things simple.
An example of using useradd in Linux would be something like this:
[root@win-puppet ~]# useradd testuser –comment ‘this is a test user’ –expiredate ‘2019-01-01’ –password ‘userpassword’
In the example above, the username is "testuser." Notice we use the format YYYY-MM-DD for the expiredate parameter. It's a requirement to work with this format, so we'll use a string in our function.
New-LocalLinuxUser code ^
Now that we see how useradd works, we can go over the simple PowerShell Core wrapper function New-LocalLinuxUser.
Here is the code for the New-LocalLinuxUser function:
/usr/sbin/useradd $Name --comment $Description --expiredate $AccountExpires
if ($LASTEXITCODE -ne 0)
Write-Error -Message "Error creating $Name"
echo $Password.GetNetworkCredential().Password | passwd --stdin $Name
if ($LASTEXITCODE -ne 0)
Write-Error -Message "Error setting password for $Name"
To hide the password used at the -Password parameter in the shell, I used the PSCredential type. You will see I use the GetNetworkCredential method to draw out the password used within the function. To set the password, you'll have to use the Get-Credential cmdlet along with the New-LocalLinuxUser command. Of course we only need the password from the credential, so I'll show how to do that below.
In addition, you'll notice in the function I use the passwd command to change the password after creating the account. This is the standard Linux command to change a user's password.
Using New-LocalLinuxUser ^
In this example, I have a user "Dan," the expiration date "2019-12-01," a password, and the description for the account, which will be "Standard account."
I specify -UserName dan in Get-Credential, so there is no prompt to input in the username. Since we are ultimately just using the password, it doesn't matter what the username actually is.
After running the command, running the command id dan shows the user's creation. By default, the user receives the user identifier (UID) and group identifier (GID) 1003.
[root@danscomputer ~]# id dan
uid=1003(dan) gid=1003(dan) groups=1003(dan)
Creating and using the New-LocalLinuxUser function is a good example of how you can use PowerShell Core to create wrappers around Linux commands to make using Linux similar to Windows. I imagine the PowerShell community will begin creating modules that can do similar tasks like this one to extend the capabilities of PowerShell Core better.