We’re going to continue to use the script we’ve been writing. Let’s make sure that we have our random users created on the system. Go ahead and run your script without the -Undo flag.

.\\Gen_Users.ps1 .\\userlist.json

Untitled

We’ll add yet another parameter by changing the first line of our script so it now looks like this:

param ([Parameter(Mandatory = $true)] $JSONFile, [switch]$Undo,  [string]$ChangePassword)

Untitled

At this point, a user could run the script like this. (This is just an example)

.\\Gen_Users.ps1 .\\userlist.json -ChangePassword NewUserPassword

Let’s create a function called ChangeUserPassword() that changes each password for the users in our .JSON file. We’ll place right under our RemoveUsers() function.

function ChangeUserPassword() {
    param ([Parameter(Mandatory = $true)] $userObject)
    $name = $userObject.$name
    Set-LocalUser -Name $name -Password (ConvertTo-SecureString $ChangePassword -AsPlainText -Force)
}

Untitled

Now we just need to call it if the user has decided to utilize the -ChangePassword parameter. At the end of our script, let’s do it like this:

foreach ($new_object in $users) {
    if ($Undo) {
        RemoveUsers($new_object)
    }
    else {
        if ($ChangePassword) {  
            ChangeUserPassword($new_object)
            
        }
        else {
            CreateNewUser($new_object)
        }
         
    }

Untitled

Now if we run this script, every user in the .json file will have the password changed to whatever the user supplies. Run the command like this.

.\\Gen_Users.ps1 .\\userlist.json -ChangePassword newPassword

Checkpoint: Screenshot that the command above runs without errors!