Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Configure Local User Account with DSC

Posted on April 16, 2014April 15, 2014

talkbubble Yesterday I posted an article on how to use PowerShell and the [ADSI] type accelerator to set a local user account. However, if you are running PowerShell 4.0 you have another option: Desired State Configuration (DSC).

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

I'm going to assume you have some basic understanding of how DSC works. If not, head over to the Public OneDrive folder for PowerShell.org and grab a copy of the free DSC ebook.

DSC ships with a provider resource for user accounts.

dsc-userresource

Because of the account password, the official stance is to use a certificate to handle encrypting the password. You can read about that on the PowerShell team blog. But that's more than I want to deal with right now, plus I trust the security of my local test network so my configurations will store the passwords as plain text in the resulting MOFs. I suppose I should show you the script I came up with.

#requires -version 4.0
#requires -RunasAdministrator

param(
[Parameter(Position=0,Mandatory)]
[ValidatePattern("^CHI")]
[string[]]$Computername
)

Configuration LocalUserAccounts {

Param(
[Parameter(Position=0,Mandatory)]
[ValidatePattern("^CHI")]
[string[]]$Computername,
[Parameter(Position=1,Mandatory)]
[PScredential]$Password
)

Node $Computername {

User LocalAdmin {
    UserName="localadmin"
    Description="Local administrator account"
    Disabled=$False
    Ensure="Present"
    Password=$Password
}

#add the account to the Administrators group
Group Administrators {
    GroupName="Administrators"
    DependsOn="[User]LocalAdmin"
    MembersToInclude="localadmin"
}

} #node

} #end configuration

#create config data to allow plain text passwords
$ConfigData=@{AllNodes=$Null}

#initialize an array for node information
$nodes=@()
foreach ($computer in $computername) {
  #write-host "Adding $computer" -foreground green
  #define a hashtable for each computername and add to the nodes array
  $nodes+=@{
          NodeName = "$computer"
          PSDscAllowPlainTextPassword=$true
        }
}

#add the nodes to AllNodes
$ConfigData.AllNodes = $nodes 

#you will be prompted to enter a credential
Write-Host "Enter the credential for localadmin" -foregroundcolor green

#create the configurations
localuseraccounts $computername -configurationdata $configdata -OutputPath c:\scripts\LocalUserAccounts

This script is intended to define a set of MOFs for computers in my Globomantics domain that all start with "CHI". The script takes an array of computernames. From there it defines the configuration, defines the necessary configuration data to allow plain text passwords and then executes the configuration. The configuration also has a Group resource to add the account to the local Administrators group. Note the DependsOn setting in the group configuration. This ensures that the account will be set up before adding it to the group.

To create the configurations I run my script specifying the computer names.
config-localpassword-dsc

PowerShell will prompt me for the credentials. When finished I am left with a MOF file for each computer under C:\Scripts\LocalUserAccounts, because I specified an output path. When I'm ready, I can push the configuration to the servers:

Start-DSCConfiguration -path c:\scripts\localuseraccounts

And that's it! I can verify using the NET USER command in a remote session.

verify-dsc-user

DSC promises to change the way IT Pros get their work done, and in a positive way!


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

3 thoughts on “Configure Local User Account with DSC”

  1. jvierra says:
    April 17, 2014 at 11:24 am

    I was just wondering what we could leverage DSC to do. This is a great idea. Use DSC to manage those troublesome local settings like local accounts.

    Thanks Jeff

    1. Jeffery Hicks says:
      April 17, 2014 at 11:59 am

      Be aware that my example is putting the password in clear text in the MOF. The better way uses certificates and encryption.

      1. jvierra says:
        April 17, 2014 at 12:50 pm

        I know but the idea is worth it. We can use DSC for other local account issue that can be a headache to maintain or change.

        It would be nice if Microsoft extended GP to use some elements of DSC. Managing change is important. It needs to be effortless.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d