Skip to main content

Setting an Account Expiration Date in Active Directory using PowerShell

 In this blog post, we will walk through a simple PowerShell script that allows you to set an account expiration date for a user in Active Directory.

The Script

Here is the PowerShell script:

# Prompt the user to enter the username
$username = Read-Host "Enter the username:"

# Prompt the user to enter the account expiration date in the specified format
$dateString = Read-Host "Enter the account expiration date in the format 'MM/dd/yyyy hh:mm AM/PM':"

# Convert the user input into a DateTime object using the ParseExact method
$time = [DateTime]::ParseExact($dateString, "MM/dd/yyyy hh:mm tt", $null)

# Get the AD user object for the specified username and retrieve the AccountExpirationDate property
$user = Get-ADUser -Identity $username -Properties "AccountExpirationDate"

# Set the AccountExpirationDate property of the user account to the specified date and time
$user.AccountExpirationDate = $time

# Update the user account in Active Directory with the new AccountExpirationDate value
Set-ADUser -Instance $user

How It Works

  1. Prompt for Username: The script begins by prompting the user to enter a username. This is the username of the account for which the expiration date will be set.

  2. Prompt for Expiration Date: Next, the script prompts the user to enter an account expiration date in the format ‘MM/dd/yyyy hh:mm AM/PM’.

  3. Convert Input to DateTime: The user input is then converted into a DateTime object using the ParseExact method.

  4. Retrieve User Object: The script retrieves the Active Directory (AD) user object for the specified username and the AccountExpirationDate property.

  5. Set Expiration Date: The AccountExpirationDate property of the user account is set to the specified date and time.

  6. Update User Account: Finally, the user account in Active Directory is updated with the new AccountExpirationDate value.

Conclusion

This script provides a simple and effective way to set an account expiration date for a user in Active Directory. However, please ensure you have the necessary permissions to execute these commands in your environment. Also, error handling is not present in this script, so you might want to add that for a production environment. Happy scripting!

Comments

Popular posts from this blog

Domain Join and Samba Configuration

# Domain Join and Samba Configuration This document provides instructions for setting up a Linux machine to join an Active Directory (AD) domain, configure Kerberos authentication, set up Samba for file sharing, and use SSSD for managing domain users. ## Prerequisites Ensure the following packages are installed on the system: - `sssd`: Manages domain authentication and users. - `cifs-utils`: Mounts and manages SMB/CIFS shares. - `chrony`: Synchronizes system time with servers. - `samba`: Provides file and printer sharing. - `samba-client`: Access and manage Samba shares. - `samba-common`: Common Samba files and configuration. - `samba-winbind`: Connects AD users and groups. - `samba-winbind-clients`: Tools for AD authentication support. - `krb5-workstation`: Kerberos client tools for authentication. ### Install Required Packages sudo dnf install realmd sssd cifs-utils chrony samba samba-client samba-common samba-winbind samba-winbind-clients krb5-workstation ## Configuration Files ### ...

Ansible : Installation and Configuration on Rocky Linux Using Python

Installing and Configuring Ansible on Rocky Linux using Python This document will guide you through the process of installing and configuring Ansible on Rocky Linux using Python. Ansible is an open-source automation tool that allows you to automate IT tasks, configuration management, application deployment, and more. Prerequisites Before we begin, make sure you have the following prerequisites in place: A running instance of Rocky Linux. Python installed on your Rocky Linux server. Python 3 is preferred. Note - Using CentOS 7 for this is possible but requires installation of python 3.9 or above is required. Also, this upgrade from python 2.7.5(preinstalled version) will come along with updating paths/SSL certificate and such. So, I opted for Rocky Linux which has latest updates. Step 1: Update System Packages Let's start by ensuring that the system packages are up to date: sudo yum update Step 2: Check and Install Python and Pip Ansible requires Python on the host mach...