What It Does
This script changes things. For every missing user who fits in this run's batch, it creates the cloud user (enabled, with a random initial password that must be changed at first sign-in), assigns the license in LicenseSku when you set one, and adds the user to each group in AddToGroups. It never deletes or changes an existing user, never touches anyone outside the CSV and never sets a manager.
The whole file is checked before anything is created. A bad row, a duplicate name, a domain the tenant has not verified, a license that is unknown or known to have too few free seats for the batch, or a group that cannot be changed from here (dynamic, synced from on-premises, role-assignable or mail-enabled) stops the run with nothing created. A license needs a usage location, from the UsageLocation column or DefaultUsageLocation. The file must be UTF-8 and at most 200 rows.
Each password is made here from the cryptographic generator and written once, before any user is created, to the secure RMM field named in ResultField, as one value of user=password pairs separated by spaces. The run refuses to create anyone without a secure field, and OutputFormat Answer is refused. A password is never printed, logged or exported.
A field holds about 240 characters, so one run creates a batch that fits, about four or five users, and lists the rest as Deferred. Copy the passwords out, then run again for the next batch. A run that creates the next batch overwrites the field with new passwords. The run summary is never written to that field, even if no users are created.
It is a dry run until DryRun=false and Approve=new-m365-users-from-csv are both set. Every create, license and group add counts toward MaxChanges (default 25), so raise it for a longer file. Users that already exist are skipped, and when all of them exist it reports already created. If a run stops part way through a user, finish that user by hand: a second run will skip them.
Before You Run It
- Create an app registration with User.Create, LicenseAssignment.ReadWrite.All, GroupMember.ReadWrite.All and Directory.Read.All, and grant admin consent.
- Create a secure RMM custom field for the passwords and name it in ResultField.
- Save the CSV as UTF-8 with a header row, and make sure every domain in it is verified in the tenant.
- Set DefaultUsageLocation, or fill the UsageLocation column, if you assign a license.
- Run a dry run first and read every row.
- Raise MaxChanges for a long file, and copy the passwords out of the field before you run the next batch.
Type and Risk
- Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
- Risk: High. It is a dry run unless you set both DryRun=false and Approve=new-m365-users-from-csv, and it stops before making more than 25 changes in one run unless you raise MaxChanges. Run it once as a dry run and read the list first.
- What it changes: Creates Entra ID users; assigns a licence; adds the users to groups; writes the initial passwords to a secure RMM field.
- Restart: It never restarts the machine.
- Typical run: Under 3 minutes.
- Example answer: "Created 4 user(s) (4 licensed, 8 group memberships); passwords are in the secure field; 2 already existed"
- Tested before listing with Windows PowerShell 5.1, run as SYSTEM, against its own test cases and a Microsoft 365 Business Premium test tenant.
Inputs
Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.
- TenantId: The client tenant: its ID (a GUID) or a domain such as contoso.onmicrosoft.com. Required.
- ClientId: The app registration's Application (client) ID, a GUID. Required.
- CertificateThumbprint: Thumbprint (40 hex characters) of the app's certificate with its private key in LocalMachine\My or CurrentUser\My on this computer. Preferred. When empty, the secret is read from the environment variable ClientSecret (a secure RMM field).
- CsvPath: Full path of the CSV file on this computer. Required.
- DefaultUsageLocation: Two-letter country code used for rows with no UsageLocation, such as CA. Default empty.
- LicenseSku: The licence to assign to every new user: the SKU part number (such as O365_BUSINESS_PREMIUM) or the SKU ID (a GUID). Default empty: no licence.
- AddToGroups: Groups every new user joins: display names or group IDs, separated by comma or semicolon. Security or Microsoft 365 groups only. Default empty.
Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.