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-08T17:06:03.04+00:00

    Ok so let's try it another way by using event log of user profile :

    $UserLogon = Get-WinEvent -LogName  'Microsoft-Windows-User Profile Service/Operational' `
    | Where-Object {$_.Id -eq 2} | Select-Object -First 1
    
    $LastUser = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$($UserLogon.UserId.Value)" -Name "ProfileImagePath"
    $substringIndex = $LastUser.ProfileImagePath.LastIndexOf("\") + 1
    $LastUserName = $LastUser.ProfileImagePath.Substring($substringIndex)
    $a=$LastUserName+"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?


  2. PerserPolis-1732 1,951 Reputation points
    2022-02-08T16:11:00.263+00:00

    No I dont have any users or services UMFD-1.

    the following script does work only if I run it locally

    **$regexsession = '.+Domain="(.+)",Name="(.+)"$'
    **$session_user = Get-WmiObject -Class win32_LoggedOnUser | Select Antecedent -Unique
    foreach($session in $session_user) {
    if($session.antecedent -match $regexsession) {

    $a="$($Matches[2])Admin"
    }
    }
    $a=$env:username+"Admin"
    New-LocalUser -AccountNeverExpires:$true -Password ( ConvertTo-SecureString -AsPlainText -Force 'User@123') -Name $a -FullName "Local Administrator" -Description "Local User Administrator" | Add-LocalGroupMember -Group administrators****

    Thank you for help

    Was this answer helpful?

    0 comments No comments

  3. PerserPolis-1732 1,951 Reputation points
    2022-02-08T15:37:55.567+00:00

    Hi,

    Running the following script

    **$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**

    The Power Shell scripts has created the user called ****"UMFD-1Admin"**

    That script is working, but still created "AdministratorAdmin"

    **$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"
    }
    }
    $a=$env:username+"Admin"

    New-LocalUser -AccountNeverExpires:$true -Password ( ConvertTo-SecureString -AsPlainText -Force 'User@123') -Name $a -FullName "Local Administrator" -Description "Local User Administrator" | Add-LocalGroupMember -Group administrators**

    Regards

    Was this answer helpful?


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.