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

PerserPolis-1732 1,951 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 for business | Windows Client for IT Pros | Directory services | Active Directory
Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Intune | Configuration Manager | Application
Windows for business | Windows Server | User experience | Other
0 comments No comments

Answer accepted by question author
Andreas Baumgarten 133K Reputation points MVP Volunteer Moderator
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

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Clément BETACORNE 2,501 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,

    Was this answer helpful?

    0 comments No comments

  2. PerserPolis-1732 1,951 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

    Was this answer helpful?

    0 comments No comments

  3. Clément BETACORNE 2,501 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,

    Was this answer helpful?

    0 comments No comments

  4. PerserPolis-1732 1,951 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

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.