Skip to content

Instantly share code, notes, and snippets.

@varunchandak
Created January 15, 2024 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save varunchandak/fee9711f31d13f6176530684ce7f221d to your computer and use it in GitHub Desktop.
Save varunchandak/fee9711f31d13f6176530684ce7f221d to your computer and use it in GitHub Desktop.
Azure AD User Update Script

Azure AD User Update Script

Overview

This PowerShell script is designed to connect to Azure Active Directory (Azure AD) and update user profiles by removing the company name from each user's profile. It reads email addresses from a specified CSV file and processes each user found in Azure AD.

Prerequisites

  • PowerShell 5.1 or higher
  • Azure Active Directory Module for Windows PowerShell
  • Administrator privileges for Azure AD
  • A CSV file containing the email addresses of the users to be updated, with one email address per line.

Installation

  1. Ensure PowerShell 5.1 or higher is installed on your system.
  2. Install the Azure Active Directory Module for Windows PowerShell using the following command:
    Install-Module -Name AzureAD
  3. Verify that the module is installed by running Get-Module -ListAvailable AzureAD.

Configuration

  1. Place the CSV file with the user email addresses on your machine, for example, at C:\Users\Administrator\Desktop\Test.csv.
  2. Modify the script's file path to point to the location of your CSV file if different from the above.

Usage

To run the script, follow these steps:

  1. Open PowerShell with administrative privileges.
  2. Navigate to the directory containing the script.
  3. Execute the script:
    .\scriptname.ps1
  4. The script will prompt for Azure AD administrator credentials. Enter the credentials to proceed.
  5. The script will read the email addresses from the CSV file and update each user profile in Azure AD by removing the company name.

What the Script Does

  1. Connect to Azure AD: The script starts by establishing a connection to Azure AD.
  2. Read Email Addresses: It reads email addresses from a specified CSV file.
  3. Update User Profiles: For each email address, the script finds the corresponding user in Azure AD and removes the company name from their profile.

Logging

The script outputs the progress to the console. It informs when a company name is removed for a user or if a user is not found.

Troubleshooting

  • Ensure that the Azure AD module is correctly installed.
  • Check that the CSV file path is correct and the file is formatted properly. ( Confirm that you have the necessary permissions in Azure AD to modify user profiles.
# Connect to Azure AD
Connect-AzureAD
# Read email addresses from a file
$emailAddresses = Get-Content -Path "C:\Users\Administrator\Desktop\Test.csv"
$properties = [Collections.Generic.Dictionary[[String],[String]]]::new()
$properties.Add("CompanyName", [NullString]::Value)
# Loop through each email address and remove company name
foreach ($email in $emailAddresses) {
$user = Get-AzureADUser -Filter "UserPrincipalName eq '$email'"
if ($user -ne $null) {
# Remove company name by setting the Company attribute to an empty string
Set-AzureADUser -ObjectId $user.ObjectId -ExtensionProperty $properties
Write-Host "Removed company name for user: $email"
}
else {
Write-Host "User not found: $email"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment