In this Lab We Will:

We’ll be utilizing the New-LocalUser cmdlet in our script this time. Reviewing how this cmdlet works from Lab_04 here might be a good idea.

In VSCode let’s create a new script called Gen_Users.ps1. Choose File→New File and type powershell.

Untitled

We’ll put this in the same folder as the .JSON file we generated in the last lab. Let’s call this file Gen_Users.ps1

Untitled

Here is the first code snippet we’ll utilize:

Here is the code to start:

param ([Parameter(Mandatory = $true)] $JSONFile)
#Comment: This can be put at the top of PowerShell scripts to indicate
#Comment: that a Parameter must be used

$users = get-content $JSONFile | convertFrom-json
#Comment: This line gets the content of the $JSONFile and loads it into a variable
$users
#Comment: Here we are printing the variable to test that it works.

Untitled

If you click the green arrow to run this file (try it now) it will ask you to provide a parameter.

Untitled

Quit the prompt with CTRL-C

CTRL-C

To run this script, we’ll have to do it from the command line. At your PowerShell prompt, you can run the script like this:

.\\Gen_users.ps1 .\\userlist.json

Untitled