{"id":5130,"date":"2023-05-22T08:08:54","date_gmt":"2023-05-22T06:08:54","guid":{"rendered":"https:\/\/www.msb365.blog\/?p=5130"},"modified":"2023-07-08T11:48:18","modified_gmt":"2023-07-08T09:48:18","slug":"powershell-script-azure-active-directory","status":"publish","type":"post","link":"https:\/\/www.msb365.blog\/?p=5130","title":{"rendered":"Add BULK Users from CSV to Azure AD Group"},"content":{"rendered":"<p>This script is for the following use case:<\/p>\n<p>An administrator needs to add multiple users from an organisation to an Azure AD group.<\/p>\n<p>This can be done in different ways. On the one hand in the Azure Admin Portal or via Powershell.<\/p>\n<p>\u00a0<\/p>\n<p>In my variant, I have created a script that can massively simplify and speed up this process.<\/p>\n<p>This article serves as documentation for my script, which you can find on my GitHub page.<\/p>\n<p>\u00a0<\/p>\n<h3>Preparation<\/h3>\n<p>\u00a0<\/p>\n<p>My approach is that all users who are to be added to a group are recorded in a CSV. The CSV needs the following format: <strong>\u201cUserPrincipalName\u201d, \u201cDisplayName\u201d<\/strong>.<\/p>\n<p>The administrator can now create a corresponding CSF manually or export an extract from the current Azure Active Directory.<\/p>\n<p>To do this, he can use the following script:<\/p>\n<pre># Split path\r\n\r\n$Path = Split-Path -Parent \"C:\\MDM\\*.*\"\r\n\r\n# Create variable for the date stamp in log file\r\n\r\n$LogDate = Get-Date -f yyyyMMddhhmm\r\n\r\n\r\n# Define CSV and log file location variables\r\n\r\n# They have to be on the same location as the script\r\n\r\n$Csvfile = $Path + \"\\AllAzADUsers_$logDate.csv\"\r\n\r\n\r\n# Get all Azure AD users\r\n\r\n$AzADUsers = Get-AzureADUser -All $true | Select-Object -Property *\r\n\r\n# Display progress bar\r\n\r\n$progressCount = 0\r\n\r\nfor ($i = 0; $i -le $AzADUsers.Count; $i++) {\r\n\r\nWrite-Progress `\r\n\r\n-Id 0 `\r\n\r\n-Activity \"Retrieving User \" `\r\n\r\n-Status \"$progressCount of $($AzADUsers.Count)\" `\r\n\r\n-PercentComplete (($progressCount \/ $AzADUsers.Count) * 100)\r\n\r\n$progressCount++\r\n\r\n}\r\n\r\n# Create list\r\n\r\n$AzADUsers | Sort-Object GivenName | Select-Object `\r\n\r\n@{Label = \"DisplayName\"; Expression = { $_.DisplayName } },\r\n\r\n@{Label = \"UserPrincipalName\"; Expression = { $_.UserPrincipalName } }|\r\n\r\n\r\n# Export report to CSV file\r\n\r\nExport-Csv -Encoding UTF8 -Path $Csvfile -NoTypeInformation #-Delimiter \";\"<\/pre>\n<p>\u00a0<\/p>\n<p>After that, the actual script can be executed. The following is a step-by-step documentation:<\/p>\n<p>\u00a0<\/p>\n<h3>AddUserToAADGroup<\/h3>\n<p>\u00a0<\/p>\n<p>It is important to note that this script currently only works with security groups!<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"209\">In the Azure AD portal, we first identify the group that is to receive several new members.<\/p>\n<p>In this example, it is the group \u201csg-HR\u201d.<\/p>\n<p>As we can see, this group currently has no members.<\/td>\n<td width=\"392\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-5131\" src=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124404.png\" alt=\"\" width=\"1293\" height=\"1239\" srcset=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124404.png 1293w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124404-300x287.png 300w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124404-1024x981.png 1024w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124404-768x736.png 768w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124404-905x867.png 905w\" sizes=\"(max-width: 1293px) 100vw, 1293px\" \/><\/td>\n<\/tr>\n<tr>\n<td width=\"209\">Now we start the PowerShell console and connect to our tenant with the command \u201cConnect-AzureAD\u201d.<\/td>\n<td width=\"392\"><img decoding=\"async\" class=\"alignnone size-full wp-image-5132\" src=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-115910.png\" alt=\"\" width=\"1290\" height=\"783\" srcset=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-115910.png 1290w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-115910-300x182.png 300w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-115910-1024x622.png 1024w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-115910-768x466.png 768w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-115910-905x549.png 905w\" sizes=\"(max-width: 1290px) 100vw, 1290px\" \/><\/td>\n<\/tr>\n<tr>\n<td width=\"209\">After we have started the script, we are asked to accept the EULA.<\/td>\n<td width=\"392\"><img decoding=\"async\" class=\"alignnone size-full wp-image-5133\" src=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-115936.png\" alt=\"\" width=\"1249\" height=\"803\" srcset=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-115936.png 1249w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-115936-300x193.png 300w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-115936-1024x658.png 1024w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-115936-768x494.png 768w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-115936-905x582.png 905w\" sizes=\"(max-width: 1249px) 100vw, 1249px\" \/><\/td>\n<\/tr>\n<tr>\n<td width=\"209\">When the EULA has been accepted, a new pop-up window appears where we can enter the name (not UPN) of the desired group.<\/p>\n<p>\u00a0<\/p>\n<p>Please enter the corresponding entry here and confirm with OK.<\/td>\n<td width=\"392\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5134\" src=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120015.png\" alt=\"\" width=\"1166\" height=\"668\" srcset=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120015.png 1166w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120015-300x172.png 300w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120015-1024x587.png 1024w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120015-768x440.png 768w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120015-905x518.png 905w\" sizes=\"(max-width: 1166px) 100vw, 1166px\" \/><\/td>\n<\/tr>\n<tr>\n<td width=\"209\">After we have defined and confirmed the group, a new window appears, now we can simply browse to the desired CSV file, select it and confirm with \u201cOpen\u201d.<\/td>\n<td width=\"392\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5135\" src=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120109.png\" alt=\"\" width=\"1467\" height=\"932\" srcset=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120109.png 1467w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120109-300x191.png 300w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120109-1024x651.png 1024w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120109-768x488.png 768w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120109-905x575.png 905w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120109-1320x839.png 1320w\" sizes=\"(max-width: 1467px) 100vw, 1467px\" \/><\/td>\n<\/tr>\n<tr>\n<td width=\"209\">The content of the imported CSV file is now displayed in the PowerShell window. Here we have the opportunity to check again whether we have selected the correct file.<\/p>\n<p>If this is not the case, simply continue with \u201cN\u201d and we can select another CSV file.<\/p>\n<p>\u00a0<\/p>\n<p>However, if it is the right file, we continue with \u201cY\u201d.<\/td>\n<td width=\"392\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5136\" src=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120143.png\" alt=\"\" width=\"1220\" height=\"808\" srcset=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120143.png 1220w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120143-300x199.png 300w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120143-1024x678.png 1024w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120143-768x509.png 768w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-120143-905x599.png 905w\" sizes=\"(max-width: 1220px) 100vw, 1220px\" \/><\/td>\n<\/tr>\n<tr>\n<td width=\"209\">Now all users from the CSV file are added to the group defined to begin.<\/td>\n<td width=\"392\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5137\" src=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124258.png\" alt=\"\" width=\"1159\" height=\"773\" srcset=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124258.png 1159w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124258-300x200.png 300w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124258-1024x683.png 1024w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124258-768x512.png 768w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124258-905x604.png 905w\" sizes=\"(max-width: 1159px) 100vw, 1159px\" \/><\/td>\n<\/tr>\n<tr>\n<td width=\"209\">After the process is complete, we see that the group has 28 new users and these are the users we had in our CSV.<\/td>\n<td width=\"392\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5138\" src=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124428.png\" alt=\"\" width=\"1287\" height=\"1242\" srcset=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124428.png 1287w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124428-300x290.png 300w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124428-1024x988.png 1024w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124428-768x741.png 768w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124428-905x873.png 905w\" sizes=\"(max-width: 1287px) 100vw, 1287px\" \/><\/td>\n<\/tr>\n<tr>\n<td width=\"209\">Of course, we can also check this further.<\/p>\n<p>\u00a0<\/p>\n<p>Basically, the task is now completed.<\/td>\n<td width=\"392\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5139\" src=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124450.png\" alt=\"\" width=\"1282\" height=\"1239\" srcset=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124450.png 1282w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124450-300x290.png 300w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124450-1024x990.png 1024w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124450-768x742.png 768w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-16-124450-905x875.png 905w\" sizes=\"(max-width: 1282px) 100vw, 1282px\" \/><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<h3>PowerShell Script on GitHub<\/h3>\n<p>\u00a0<\/p>\n<p>The script itself can be found on my GitHub repository under the following link<\/p>\n<p><a href=\"https:\/\/github.com\/MSB365\/AzureADGroup-MGMT\/tree\/main\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5142\" src=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/GitHub-Mark.png\" alt=\"\" width=\"255\" height=\"255\" srcset=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/GitHub-Mark.png 560w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/GitHub-Mark-300x300.png 300w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/GitHub-Mark-150x150.png 150w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2023\/05\/GitHub-Mark-500x500.png 500w\" sizes=\"(max-width: 255px) 100vw, 255px\" \/><\/a><\/p>\n<p>\u00a0<\/p>\n<h3>Conclusion<\/h3>\n<p>The script can be used free of charge as described in the licence conditions. Changes are also allowed. However, when distributing or using the individual modules, reference must be made to this article and the MSB365 blog.<\/p>\n<p>I strongly recommend testing the script in a LAB the first time you use it.<\/p>\n<p>Likewise, I assume no liability for possible damage that the script may cause.<\/p>\n<p>It is not allowed to pass on this script or single modules to third parties against payment. We follow the approach: From the community, for the community.<\/p>\n<p>\u00a0<\/p>\n<p>\u2764\ufe0f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This script is for the following use case: An administrator needs to add multiple users from an organisation to an Azure AD group. This can be done in different ways. On the one hand in the Azure Admin Portal or via Powershell. \u00a0 In my variant, I have created a script that can massively simplify [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5145,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[12,1923,3],"tags":[],"class_list":["post-5130","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure","category-microsoft-365","category-powershell"],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.msb365.blog\/index.php?rest_route=\/wp\/v2\/posts\/5130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.msb365.blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.msb365.blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.msb365.blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.msb365.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5130"}],"version-history":[{"count":4,"href":"https:\/\/www.msb365.blog\/index.php?rest_route=\/wp\/v2\/posts\/5130\/revisions"}],"predecessor-version":[{"id":5144,"href":"https:\/\/www.msb365.blog\/index.php?rest_route=\/wp\/v2\/posts\/5130\/revisions\/5144"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.msb365.blog\/index.php?rest_route=\/wp\/v2\/media\/5145"}],"wp:attachment":[{"href":"https:\/\/www.msb365.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.msb365.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.msb365.blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}