How to Enable/Disable users in bulk via powershell

Configurare noua (How To)

Situatie

Here’s a PowerShell script to enable or disable multiple user accounts in bulk:

Solutie

# Path to the CSV file containing user details (e.g., SamAccountName, Action)
$csvFilePath = “C:\Path\to\users.csv”

# Import user details from the CSV file
$users = Import-Csv $csvFilePath

# Loop through each user and perform the specified action (Enable/Disable)
foreach ($user in $users) {
$username = $user.SamAccountName
$action = $user.Action

# Check if the user exists
if (Get-ADUser -Filter {SamAccountName -eq $username}) {
# Enable or Disable the user based on the specified action
if ($action -eq “Enable”) {
Enable-ADAccount -Identity $username
Write-Host “User $username enabled”
}
elseif ($action -eq “Disable”) {
Disable-ADAccount -Identity $username
Write-Host “User $username disabled”
}
else {
Write-Host “Invalid action specified for user $username”
}
}
else {
Write-Host “User $username not found”
}
}

Before running the script, ensure you have a CSV file (e.g., “users.csv”) containing at least two columns: “SamAccountName” and “Action”. Each row in the CSV file should contain the SamAccountName of a user and the corresponding action to perform (Enable or Disable).

Example CSV file (users.csv):

SamAccountName,Action
user1,Enable
user2,Disable
user3,Enable

Replace “C:\Path\to\users.csv” with the actual path to your CSV file.

This script will read user details from the CSV file, loop through each user, check if the user exists in Active Directory, and perform the specified action (Enable or Disable) on the user account. If the user does not exist, it will display a message indicating that the user was not found.

Tip solutie

Permanent

Voteaza

(9 din 16 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?