Below is a PowerShell script that reads user information from a CSV file and sets the account expiration date to two days from the current date for each user in Active Directory. Please make sure to adjust the CSV file structure according to your needs:

CSV structure:

# Import the Active Directory module
Import-Module ActiveDirectory

# Specify the path to the CSV file containing user information
$csvFilePath = "C:\Path\To\Your\Users.csv"

# Read user information from the CSV file
$userData = Import-Csv $csvFilePath

# Function to set account expiration date for AD user
function Set-ADUserExpiryDate {
    param (
        [string]$username,
        [int]$daysToAdd
    )

    $user = Get-ADUser -Identity $username

    if ($user) {
        $expiryDate = (Get-Date).AddDays($daysToAdd)
        Set-ADAccountExpiration -Identity $username -DateTime $expiryDate
        Write-Host "Account expiration date set for $username: $expiryDate"
    } else {
        Write-Host "User $username not found in Active Directory."
    }
}

# Loop through each user in the CSV and set the expiration date
foreach ($user in $userData) {
    Set-ADUserExpiryDate -username $user.Username -daysToAdd 2
}


Please ensure that your CSV file (Users.csv) has a header row with the column Username (case-sensitive) containing the usernames for which you want to set the expiration date.

Note: Make sure to test scripts in a safe environment before applying them to a production Active Directory. Ensure that you have the necessary permissions to modify user account information.

By amit_g

Welcome to my IT Infra Blog! My name is Amit Kumar, and I am an IT infrastructure expert with over 11 years of experience in the field. Throughout my career, I have worked with a wide variety of systems and technologies, from network infrastructure and cloud computing to hardware and software development. On this blog, I aim to share my knowledge, insights, and opinions on all things related to IT infrastructure. From industry trends and best practices to tips and tricks for managing complex systems, my goal is to provide valuable information that will help IT professionals and enthusiasts alike. Whether you are a seasoned IT veteran or just getting started in the field, I hope you will find my blog to be a valuable resource. In addition to sharing my own thoughts and ideas, I also welcome feedback, comments, and questions from my readers. I believe that a collaborative approach is the best way to advance the field of IT infrastructure and I look forward to hearing from you. Thank you for visiting my blog, and I hope you will continue to follow along as I explore the fascinating world of IT infrastructure. Sincerely, Amit Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *