# Script to create a new user and add them to a role?

**URL:** <https://forum.inductiveautomation.com/t/script-to-create-a-new-user-and-add-them-to-a-role/92303>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [June 24, 2024, 8:58pm UTC](https://forum.inductiveautomation.com/t/script-to-create-a-new-user-and-add-them-to-a-role/92303 "2024-06-24T20:58:45Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Crockett](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Jason\_Crockett](https://forum.inductiveautomation.com/u/Jason_Crockett)\
**Post date:** [June 24, 2024, 8:58pm UTC](https://forum.inductiveautomation.com/t/script-to-create-a-new-user-and-add-them-to-a-role/92303/1 "2024-06-24T20:58:45Z")

</div>

I am the worst with scripting. I am sure someone has already created a working script to create a new user and add them to a role.

I've been trying all day but have not moved forward at all. I've used many posts here in the forum. I am using all default settings in my GW.

Would someone Pleeease share their working script with me?

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 24, 2024, 9:03pm UTC](https://forum.inductiveautomation.com/t/script-to-create-a-new-user-and-add-them-to-a-role/92303/2 "2024-06-24T21:03:23Z")

</div>

Check out the example script given in the manual's documentation  
[system.user.getNewUser | Ignition User Manual (inductiveautomation.com)](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-user/system-user-getNewUser)

---

<div class="post-metadata">

**Author:** ![Jason\_Crockett](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Jason\_Crockett](https://forum.inductiveautomation.com/u/Jason_Crockett)\
**Post date:** [June 24, 2024, 9:15pm UTC](https://forum.inductiveautomation.com/t/script-to-create-a-new-user-and-add-them-to-a-role/92303/3 "2024-06-24T21:15:25Z")

</div>

I appreciate the response.

I have seen and attempted to use that information to no avail. To be honest, I don't even know where to start besides the script console tool in Designer. I've been spinning my wheels.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 24, 2024, 9:21pm UTC](https://forum.inductiveautomation.com/t/script-to-create-a-new-user-and-add-them-to-a-role/92303/4 "2024-06-24T21:21:43Z")

</div>

Post what you've tried and we can tell you what's wrong with it. Even if someone gives you something that works perfectly, cargo-culting scripts that way is an easy recipe for unmaintainable systems down the road.

We could also try this: Why are you reaching to scripting to perform this operation? If it's a one-off, the user interface on the gateway webpage should be sufficient. If you want it in your visualization system (Vision, Perspective) then you should be able to use either the built-in GUI tools or [this Exchange resource](https://inductiveautomation.com/exchange/2128/overview). Scripting would only be _necessary_ if you want to perform this "create user with roles operation" _automatically_ based on some trigger condition (which?).

---

<div class="post-metadata">

**Author:** ![Jason\_Crockett](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Jason\_Crockett](https://forum.inductiveautomation.com/u/Jason_Crockett)\
**Post date:** [June 24, 2024, 9:35pm UTC](https://forum.inductiveautomation.com/t/script-to-create-a-new-user-and-add-them-to-a-role/92303/5 "2024-06-24T21:35:08Z")

</div>

Attached are 2 that I've been working on.

[NewUser.py](https://forum.inductiveautomation.com/uploads/short-url/aNqwnU5PmlAUDkHFUWkVdrwNwLI.py) (878 Bytes)

[NewUser\_attempt2.txt](https://forum.inductiveautomation.com/uploads/short-url/2NluqUrERv4m0iMSqubYHY0LIh0.txt) (1.1 KB)

---

<div class="post-metadata">

**Author:** ![Jason\_Crockett](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Jason\_Crockett](https://forum.inductiveautomation.com/u/Jason_Crockett)\
**Post date:** [June 24, 2024, 9:37pm UTC](https://forum.inductiveautomation.com/t/script-to-create-a-new-user-and-add-them-to-a-role/92303/6 "2024-06-24T21:37:10Z")

</div>

I forgot to add...

I would like to use a script due to the fact that I need to update user accounts on approximately 40 Ign servers. I thought it would speed things up by using a script but with all the time I have wasted today, I probably could have had it done by now. Haha

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 24, 2024, 10:01pm UTC](https://forum.inductiveautomation.com/t/script-to-create-a-new-user-and-add-them-to-a-role/92303/7 "2024-06-24T22:01:55Z")

</div>

Okay. Well, step 0 is "stop trusting ChatGPT". It doesn't know enough about Ignition to be helpful, and instead will lead you down a thousand dark alleys, confidently taking advantage of the fact that you don't know what you're doing yet.

I slightly modified the example on the page @dkhayes117 linked above.

Paste this into your script console, and confirm that it's working.

```python
# Util for printing the responses.
def printResponse(responseList):
    if len(responseList) > 0:
        for response in responseList:
            print response
    else:
        print " None"
  
# Make a brand new 'blank' user.
user = system.user.getNewUser("", "ignuser")
  
# Let's fill in some fields. Note we have two ways to access property names.
user.set("firstname", "Naomi")
user.set(user.LastName, "Nagata")
user.set("password", "1234567890")

# We can add a role. If the role doesn't already exist, user save will fail, depending on user source.
user.addRole("Administrator")
  
# # We can add a lot of roles.
# roles = ["Administrator", "Operator"]
# user.addRoles(roles)

# Finally, we will save our new user and print responses.
response = system.user.addUser("", user)
  
print "Warnings are:"
printResponse(response.warns)
   
print "Errors are:"
printResponse(response.errors)

print "Infos are:"
printResponse(response.infos)

```

---

<div class="post-metadata">

**Author:** ![Jason\_Crockett](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Jason\_Crockett](https://forum.inductiveautomation.com/u/Jason_Crockett)\
**Post date:** [June 24, 2024, 10:19pm UTC](https://forum.inductiveautomation.com/t/script-to-create-a-new-user-and-add-them-to-a-role/92303/8 "2024-06-24T22:19:41Z")

</div>

Totally busted. I got desperate and did try ChatGPT. ha You're good!

With the script you provided, I am receive this error:

Errors are:  
You are not authorized to modify the Gateway system user source.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 24, 2024, 10:28pm UTC](https://forum.inductiveautomation.com/t/script-to-create-a-new-user-and-add-them-to-a-role/92303/9 "2024-06-24T22:28:27Z")

</div>

Okay, so you need to check the 'Allow User Admin' box in the gateway's security settings: [Gateway General Security Settings | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/platform/security/gateway-general-security-settings#gateway-security-settings-table)

---

<div class="post-metadata">

**Author:** ![Jason\_Crockett](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Jason\_Crockett](https://forum.inductiveautomation.com/u/Jason_Crockett)\
**Post date:** [June 24, 2024, 10:42pm UTC](https://forum.inductiveautomation.com/t/script-to-create-a-new-user-and-add-them-to-a-role/92303/10 "2024-06-24T22:42:36Z")

</div>

You are officially my Hero of the day!

Thank you soo much!

---

<div class="post-metadata">

**Author:** ![Jason\_Crockett](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Jason\_Crockett](https://forum.inductiveautomation.com/u/Jason_Crockett)\
**Post date:** [June 24, 2024, 10:50pm UTC](https://forum.inductiveautomation.com/t/script-to-create-a-new-user-and-add-them-to-a-role/92303/11 "2024-06-24T22:50:35Z")

</div>

One more question.

How can I use the script to update user information? Such as updating a password or role to an existing user?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 24, 2024, 11:12pm UTC](https://forum.inductiveautomation.com/t/script-to-create-a-new-user-and-add-them-to-a-role/92303/12 "2024-06-24T23:12:01Z")

</div>

Instead of using `getNewUser` (to...get a new user 😄) you would use `system.user.getUser` to retrieve an existing user:

> **[system.user.getUser | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-user/system-user-getUser)**
>
> Looks up a specific user in a user source, by username.

Then make the changes you want to that user, and use `system.user.editUser` as you did in the first script to apply those edits back.
