# Import the AzureAD module Import-Module AzureAD # Connect to Azure AD Write-Host "Connecting to Azure AD..." Connect-AzureAD # Define the path to the CSV file $csvPath = "C:\HRM users License Report\cleanup\DisabledAccont12Nov2024.csv" # Import the CSV file Write-Host "Importing user list from CSV..." $users = Import-Csv -Path $csvPath # Loop through each user in the CSV file and delete the account Write-Host "Starting user deletion process..." foreach ($user in $users) { $userPrincipalName = $user.UserPrincipalName # Adjust the column name if needed try { # Retrieve the user by UserPrincipalName using the filter approach $azureADUser = Get-AzureADUser -Filter "UserPrincipalName eq '$userPrincipalName'" if ($azureADUser) { # Delete the user using ObjectId Write-Host "Attempting to delete user: $userPrincipalName" Remove-AzureADUser -ObjectId $azureADUser.ObjectId Write-Host "Deleted user: $userPrincipalName" -ForegroundColor Green } else { Write-Host "User not found: $userPrincipalName" -ForegroundColor Yellow } } catch { Write-Host "Failed to delete user: $userPrincipalName. Error: $_" -ForegroundColor Red } } Write-Host "User deletion process completed."
Friday, November 15, 2024
Delete AzureAD user
Friday, January 26, 2024
Azure AD users Sign in logs for 30 days report
#PowerShell script reads the All guest account details as a CSV and check in Azure AD sign in logs 30 days and generates the report file # Install the AzureAD module (if not already installed) Install-Module -Name AzureAD -Force -AllowClobber # Import the AzureAD module Import-Module AzureAD # Connect to Azure AD (you will be prompted for credentials) Connect-AzureAD # Read CSV file $csvPath = "C:\Report\AllGuestUsers.CSV" $csvData = Import-Csv -Path $csvPath # Array to store results $results = @() # Iterate through each row in the CSV foreach ($row in $csvData) { $email = $row.email Write-Host $email -ForegroundColor Green # Get sign-in logs for the specific email $signInLogs = Get-AzureADAuditSignInLogs -Filter "startsWith(userPrincipalName, '$email')" # Check if any sign-in logs were found $status = if ($signInLogs.Count -gt 0) { 'Found' } else { 'Not Found' } # Create an object with the email and status $result = [PSCustomObject]@{ Email = $email Status = $status } # Add the result to the array $results += $result } # Export results to CSV $results | Export-Csv -Path "C:\Report\Results.csv" -NoTypeInformation # Display a confirmation message Write-Output "Results exported to Results.csv"
Disable guest account from Azure AD reading CSV file
#This PowerShell script is used to read a CSV file having list of User Principal name (not email) #and disable the account in azure AD # Install the AzureAD PowerShell module if not already installed Install-Module -Name AzureAD -Force -AllowClobber -Scope CurrentUser # Import the AzureAD module Import-Module AzureAD # Connect to Azure AD Connect-AzureAD # -Credential $credential # Specify the path to your CSV file $csvFilePath = "C:\Report\Final\UserPrinicpalList.csv" # Read the CSV file $csvData = Import-Csv -Path $csvFilePath # Iterate through each row in the CSV foreach ($row in $csvData) { $upn = $row.UserPrincipalName # Get the user object from Azure AD $azureADUser = Get-AzureADUser -Filter "UserPrincipalName eq '$upn'" if ($azureADUser) { Write-Host $email -ForegroundColor Green # Disable the user account Set-AzureADUser -ObjectId $azureADUser.ObjectId -UserPrincipalName $upn -AccountEnabled $false Write-Host "Azure AD account for $email has been disabled." } else { Write-Host $upn -ForegroundColor Red Write-Host "User with email $upn not found in Azure AD." } }
Subscribe to:
Posts (Atom)
HTML
Script: