Create a local user admin account on each computer in domain based on the name of domain user account

PerserPolis-1732 1,326 Reputation points
2022-02-08T09:26:47.31+00:00

Hi,

I want to create a local user admin account on each computer in domain client Computers based on the name of domain user account as per requirements given below

1) Set password for “localuser” as “password”
2) Make “user” the member of local administrators group
3) USer must change password at next logon

For example:

I have a domain user account and it is called "mbiden" , the local user account should be called after creating "mbidenAdmin"

That is my PowerShell script with computername and Admin, it creating a local user admin with "Computername+"Admin", but I want to create create local admin based on the domain user account name

$a=$env:computername+"Admin"
New-LocalUser -AccountNeverExpires:$true -Password ( ConvertTo-SecureString -AsPlainText -Force 'password') -Name $a -FullName "Local Administrator" -Description "Local Administrator" | Add-LocalGroupMember -Group administrators

Can anybody help me?

Regards

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,049 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,801 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,344 questions
Microsoft Configuration Manager Application
Microsoft Configuration Manager Application
Microsoft Configuration Manager: An integrated solution for for managing large groups of personal computers and servers.Application: A computer program designed to carry out a specific task other than one relating to the operation of the computer itself, typically to be used by end users.
453 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 95,026 Reputation points MVP
    2022-02-08T11:22:28.157+00:00

    Hi @PerserPolis-1732 ,

    if you are logged-in with your domain user you can try $env:USERNAME instead of $env:computername in your script.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.
    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. PerserPolis-1732 1,326 Reputation points
    2022-02-08T11:37:32.167+00:00

    Hi,

    I did change the script as following:

    $a=$env:username+"Admin"
    New-LocalUser -AccountNeverExpires:$true -Password ( ConvertTo-SecureString -AsPlainText -Force 'password') -Name $a -FullName "Local Administrator" -Description "Local Administrator" | Add-LocalGroupMember -Group administrators.

    It is working with username but only if I run it locally on the machine, but it is not working if I deploy the script on the same machine with SCCM.
    If I deploy it, it created "AdministratorAdmin"

    Regards

    0 comments No comments

  2. Clément BETACORNE 2,031 Reputation points
    2022-02-08T14:57:55.873+00:00

    Hello,

    You can try to have the information regarding the currently logged user with win32_LoggedOnUser :

    $regexsession = '.+Domain="(.+)",Name="(.+)"$'
    
    $session_user = Get-WmiObject -Class win32_LoggedOnUser | Select Antecedent -Unique
    
    foreach($session in $session_user) {
        if($session.antecedent -match $regexsession) {
            #Matches[2] will contain the username
            Write-Output $Matches[2]
        }
    }
    

    Regards,

    0 comments No comments

  3. PerserPolis-1732 1,326 Reputation points
    2022-02-08T15:01:42.893+00:00

    Hi,

    thank you for your replay.

    How does look the script with currently logged with my script?

    Regards

    0 comments No comments

  4. Clément BETACORNE 2,031 Reputation points
    2022-02-08T15:20:19.92+00:00

    Something like that :

    $regexsession = '.+Domain="(.+)",Name="(.+)"$'
    
    $session_user = Get-WmiObject -Class win32_LoggedOnUser | Select Antecedent -Unique
    
    foreach($session in $session_user) {
        if($session.antecedent -match $regexsession) {
            #Matches[2] will contain the username
            $a="$($Matches[2])Admin"
        }
    }
    
    New-LocalUser -AccountNeverExpires:$true -Password ( ConvertTo-SecureString -AsPlainText -Force 'password') -Name $a -FullName "Local Administrator" -Description "Local Administrator" | Add-LocalGroupMember -Group administrators
    

    Regards,

    0 comments No comments