23 How to set AD Expire [date/time] and update notes?

In the below PowerShell script:-

  1. Name and save the script as “Set-ADExpiryDateTime.PS1”.
  2. Run the script from the PowerShell console and provide the inputs as per the prompt.
  3. The output will be displayed within the PowerShell console as default.
In case of any hurdle, please Contact Us

 

Import-Module ActiveDirectory 
$usr=Read-Host 'Enter Username'
$date=Read-Host 'Enter ExpiryDate and Time [m/dd/yyy hh:mm:ss]'
$task= Read-Host "Enter the ServiceNow Task ID with notes [TASK0xx00xx (Expired)]"
$info = Get-ADUser $usr -Properties info | %{ $_.info} 
$user=get-aduser $usr
Set-ADAccountExpiration -Identity $usr -DateTime $date
Set-ADUser $usr -Replace @{info="$($info) $task"} 
Write-Progress -Activity "Wait for less than 11 Seconds to see the Expiry Date and Time of" -Status "$usr" -PercentComplete 50
Start-Sleep -Seconds 10.5
Import-Module ActiveDirectory 
#Get-ADUser -Identity $usr -Properties DisplayName, AccountExpirationDate, info, CanonicalName |Select-Object -Property DisplayName, SamAccountName, Enabled, AccountExpirationDate, Info, CanonicalName | Out-GridView

Get-ADPrincipalGroupMembership -Identity $usr |select @{Name="Currently Member Of"; Expression = {($_.name)}}

Get-ADUser -Identity $usr -Properties DisplayName, name, AccountExpirationDate, info, MemberOf, CanonicalName |
Select-Object -Property @{Name="Please find the information of"; Expression = {($_.Name)} }, @{Name="Full Name"; Expression = {($_.DisplayName)}}, @{Name="Username"; Expression = {($_.SamAccountName)}}, 
@{Name="AccountEnabled"; Expression = {($_.Enabled)}}, AccountExpirationDate, 
@{Name="Current-OU"; Expression = {($_.CanonicalName)}}, @{Name="MemberGroups"; Expression = {(($_.MemberOf).split(",") |
where-object {$_.contains("CN=")}).replace("CN=","")}}, @{Name="Notes"; Expression = {($_.Info)}} |
Out-Default