Ok, apparently your CSV looks like this:
AccountExpires;SamAccountname;Displayname;Company;Manager
date1;user1;name1;Co1;Boss1
date2;user2;name2;Co2;Boss2
Then:
# just take the SamAccountname column values and ignore the rest
(Import-Csv -Path 'D:\Test\Original.csv' -Delimiter ';').SamAccountname | ForEach-Object {
# calculate the account expiration date
$user = Get-ADUser -Filter "SamAccountName -eq '$_'" -Properties AccountExpirationDate, accountExpires -ErrorAction SilentlyContinue
if ($user) {
$expires = if ($user.accountExpires -gt 0 -and $user.accountExpires -ne 9223372036854775807) { $user.AccountExpirationDate }
else { 'Never Expires' }
# output an object
[PsCustomObject]@{
SamAccountName = $_
AccountExpires = $expires
}
}
else {
Write-Warning "User '$_' does not exist"
}
} | Export-Csv -Path 'D:\Test\ExpiringAccounts.csv' -NoTypeInformation
Output ExpiringAccounts.csv will be something like this when opened in Notepad:
"SamAccountName","AccountExpires"
"user1","Never Expires"
"user2","6/5/2021 4:36 PM"