Create local user account and add to group

M, RAKESH 121 Reputation points
2022-01-16T06:14:47.197+00:00

I need to create multiple Local user account and add them to groups in multiple Azure VMs(Linux and Windows) in one go.
Is there any option in Azure to do this?

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,013 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Prrudram-MSFT 28,201 Reputation points Moderator
    2022-01-17T20:40:08.69+00:00

    Hello @M, RAKESH ,

    Thank you for reaching out to Microsoft Q&A platform.

    1.For Windows, you could achieve this with the below PowerShell Script. This will automate this process. It will create a new user, add them to the "Users" group, set a temporary password of "P@ssword1", then set that password to be expired so the user will change their password when they log in. You can add the users you want to be created by adding them to the $users variable and following the example formatting.

    # The default password for the new accounts  
    $password = ConvertTo-SecureString "P@ssword1" -AsPlainText -Force  
    
    # Group new users are added to (default is Users)  
    $group = "Users"  
    
    # PowerShell array containing the users to be created  
    $users = @(  
     # List users here to be created  
     "user1"  
     "user2"  
     "user3"  
    )  
    
    # Creates a new users for each entry in the $users array  
    foreach ($user in $users) {  
    
     # Create and add user to Users group  
     New-LocalUser -Name "$user" -Password $Password  
     Add-LocalGroupMember -Group "$group" -Member "$user"  
    
     # Set users password to be changed at next logon  
     $expUser = [ADSI]"WinNT://localhost/$user,user"  
     $expUser.passwordExpired = 1  
     $expUser.setinfo()  
    
     }  
    

    165765-image.png

    now, to run this across VMs in one go, you could further leverage PS command "Invoke-Command"

    PowerShell allows you to run local PS1 scripts on remote computers. The idea is that you store the PowerShell script above in a local .PS1 file on your computer. With PowerShell Remoting, you can transfer a PS1 file to a remote computer and execute it there.

    To do this, use the -FilePath parameter in the Invoke-Command cmdlet instead of -ScriptBlock. For example, to run the c:\ps\usercreationscript.ps1 script on three remote servers, you can use the following command:

    Invoke-Command -FilePath c:\ps\usercreationscript.ps1 -ComputerName server1,server2,server3

    For more details, refer to run-powershell-script-on-remote-computer

    1. For the Linux, you can check the following link create-multiple-users-using-shell-script-in-linux

    Disclaimer: This response contains a reference to a third-party World Wide Web site. Microsoft is providing this information as convenient to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    Please "Accept as Answer" if any of above helped so that, it can help others in community looking for remediation for the similar issues.

    0 comments No comments

  2. M, RAKESH 121 Reputation points
    2022-01-19T10:21:49.807+00:00

    @Prrudram-MSFT , Thanks for your response.

    I found Microsodt document which explains executing shell script. Need to know how this invoke command can be used on multiple VMs.

    https://learn.microsoft.com/en-us/powershell/module/az.compute/invoke-azvmruncommand?view=azps-7.1.0#example-2--invoke-a-command-on-linux

    0 comments No comments

Your answer

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