🚀 Bulk-Import Users into Nextcloud via CSV + Group Assignment (Shell Edition)
Fifty new users, each with their groups, and no desire to click fifty times? A CSV file and a small script handle it in one go.

🌐 Also available in: Deutsch · Français · Español · Italiano · Português · Nederlands · Polski
In short: You want to create many users in Nextcloud at once, each with groups? This pure shell script reads them from a CSV file, creates them via occ and assigns them one or more groups. The post shows prerequisites, the CSV format, the script, usage and a few notes on passwords and groups.
So, you’re running a Nextcloud instance and want to batch-create users like a proper sysadmin? Here’s a pure shell-based solution to create users from a CSV file, assign them to groups (even multiple ones), and feel like a boss while doing it.
Table of Contents
📦 Prerequisites
- SSH access to your Nextcloud (opens in a new tab) server
- Command-line access to
occ(located in the root of your Nextcloud install, e.g./var/www/html/nextcloud) - Your CSV file ready to roll
📄 CSV File Format
No headers, just pure data. Format:
username,password,Display Name,Group1;Group2;Group3
Example:
next001,secret123,Max Mustermann,Vorstand;Mitglieder
next002,test456,Lisa Beispiel,Trainer
next003,pass789,Peter Test,Trainer;Mitglieder
🛠️ The Bash Script: create_users_with_groups.sh
#!/bin/bash
CSV_FILE="benutzer.csv" # Path to your CSV file
cd /var/www/html/nextcloud || exit 1
# Read file line by line
while IFS=',' read -r username password displayname groups_raw; do
# Skip empty or malformed lines
if [[ -z "$username" || -z "$password" || -z "$displayname" ]]; then
echo "⚠️ Skipping invalid or empty line: $username, $displayname"
continue
fi
echo "▶️ Creating user: $username ($displayname)"
# Export password into environment
export OC_PASS="$password"
# Use -E to preserve environment for www-data
sudo -E -u www-data php occ user:add --password-from-env "$username" --display-name "$displayname"
USER_EXISTS=$?
if [ $USER_EXISTS -ne 0 ]; then
echo "❌ Failed to create user '$username'. Skipping group assignment."
continue
fi
# Split group list on semicolon (;) and trim whitespace
IFS=';' read -ra groups <<< "$groups_raw"
for group in "${groups[@]}"; do
group=$(echo "$group" | xargs) # Trim spaces
if [ -n "$group" ]; then
echo " ➕ Adding $username to group '$group'"
# Create group if not already existing (ignore stderr)
sudo -u www-data php occ group:add "$group" 2>/dev/null
# Add user to group
sudo -u www-data php occ group:adduser "$group" "$username"
fi
done
done < "$CSV_FILE"
⚙️ Usage
chmod +x create_users_with_groups.sh
./create_users_with_groups.sh
Make sure your benutzer.csv file is in the same directory or update the path accordingly.
🔐 Notes
- Passwords are passed securely using environment variables – not via command line or stdin
- Group names are auto-created if they don’t already exist
- Semicolon (
;) separates multiple groups per user
🧠 Bonus Tip
Want to go full automation mode? Integrate this with user onboarding scripts or provision your users via LDAP and only use this for custom or temporary accounts.
🎯 Conclusion: Sysadmin Glory Unlocked
That’s it – your users are now live, grouped, and ready to Nextcloud like pros. Want to set up Group Folders and apply permission rules? Ping me – I’ll nerd that out too.
📚 Related posts:
