it:ad:powershell:howto:snippets:ensure_a_local_user_account

IT:AD:Powershell:HowTo:Snippets:Ensure a Local User Account

The following will set up a Local user:

function Ensure-LocalUser(){
  [CmdletBinding()]
  param(
    [Parameter(Mandatory=$true)]
    [string] $userName,
    [Parameter(Mandatory=$true)]
    [string] $password,
    [Parameter(Mandatory=$true)]
    [string] $description
  )
  process{

    $objOu = [ADSI]"WinNT://${env:Computername}"
    $localUsers = $objOu.Children | where {$_.SchemaClassName -eq 'user'}  | % {$_.name[0].ToString()}

    if($localUsers -NotContains $userName)
    {
      $objUser = $objOU.Create("User", $userName)
      $objUser.setpassword($password)
      $objUser.SetInfo()
      $objUser.description = $description
      $objUser.SetInfo()

      return $true
    }
    else
    {
      return $false
    }

  }
}

  • /home/skysigal/public_html/data/pages/it/ad/powershell/howto/snippets/ensure_a_local_user_account.txt
  • Last modified: 2023/11/04 23:20
  • by 127.0.0.1