Parsing date from CSV

Good morning my fine powershell friends! I have a csv that I am using for a new-aduser creation. One of my headers is called Wdate, which holds a date that a user should be expired. I need to add 30 days to it then it will become disabled and moved to a specific OU. I have been reading about parseExact, and although I understand what it does I am not quite sure how to implement it within the script. Any help is greatly appreciated. Below is an example of the csv, and the script I am testing on my test lab.

SamAccountName LastName FirstName Middle Pin Adult Wdate Status
N417934 Abbott Breanna K 1234 A 2020-07-02 W
S39306650 Anderson Ken G 1234 2020-08-14 W
# Import active directory module for running AD cmdlets
Import-Module activedirectory
#Store the data from ADUsers.csv in the $ADUsers variable
#$ADUsers = Import-csv "C:\Powershell\SAM_ACCT.csv"
$ADUsers = Import-csv "C:\Powershell\SAM_ACCT.csv" -header 'SAMACCOUNTNAME','LASTNAME','FIRSTNAME','MIDDLE INITIAL','PIN','ADULT STUDENT','WDATE','STATUS' | Select-Object -Property @{
Name='WDate';
Expression={
[datetime]::ParseExact($($_.wdate),'yyyyMMdd',$null)}
},SAMACCOUNTNAME,LASTNAME,FIRSTNAME,MIDDLE INITIAL,PIN,ADULT STUDENT,STATUS
$a=1; #Variable for Successful Users
$b=1; #Variable for Failed Users
$failedUsers = @()
$successUsers = @()
$VerbosePreference = "Continue"
$LogFolder = "C:\Temp\logs\Focus"
#Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers)
{
#Read user data from each field in each row and assign the data to a variable as below
$Username       = $User.SamAccountName
$Password       = $User.pin
$Firstname      = $User.firstname
$Lastname       = $User.lastname
$DisplayName    = "$LastName $FirstName"
$Name           = "$LastName $Firstname $UserName"
$Status         = $User.Status
$Disable        = $User.Wdate
#$Disable       = (Get-Date).AddDays(30)
$OU_Adult       = "OU=S_Adult,OU=Enrolled_Students,DC=testlab,DC=local"
$OU_Student     = "OU=N_Student,OU=Enrolled_Students,DC=testlab,DC=local"
$O365Base  = "O365SCTIBase"
$O365Email = "O365SCTIEmail"
$O365Base  = Get-ADGroup $O365Base
$O365Email = Get-ADGroup $O365Email
#Check if SamAccountName Starts with S or N
if ($Username -like 'S*') {
$OU = "$OU_Adult"
}
if ($Username -like 'N*') {
$OU = "$OU_Student"
}
Try {
if (!(get-aduser -Filter {samaccountname -eq "$UserName"})){
$NewUserParams = @{
'SamAccountName'    =  "$Username"
'UserPrincipalName' =  "$Username@testlab.local"
'Name'              =  "$Name"
'GivenName'         =  "$Firstname"
'Surname'           =  "$Lastname"
'Enabled'           =   $True
'DisplayName'       =  "$DisplayName"
'Path'              =   $OU
'AccountPassword'   =   (convertto-securestring $Password -AsPlainText -Force)`

}
New-AdUser @NewUserParams
Write-Verbose "[PASS] Created $DisplayName"
$successUsers += $DisplayName + "," +$UserName
if ($Status -like 'W'){
Set-ADAccountExpiration -Identity $Username -DateTime $Disable
Write-Verbose "Account $UserName will expire on $Disable."
}
}
} Catch {
Write-Warning "[ERROR]Can't create user [$($DisplayName)] : $_"
$failedUsers += $DisplayName + "," +$UserName + "," +$_
}
if ( !(test-path $LogFolder)) {
Write-Verbose "Folder [$($LogFolder)] does not exist, creating"
new-item $LogFolder -type directory -Force
}
if ($Username -like 'S*') {
# Add users to O365 Groups
Add-ADGroupMember "$O365Base" -Members "$Username"
Add-ADGroupMember "$O365Email" -Members "$Username"
}
}
Write-verbose "Writing logs"
$failedUsers   |ForEach-Object {"$($b).) $($_)"; $b++} | out-file -FilePath  $LogFolder\FailedUsers.log -Force -Verbose
$successUsers | ForEach-Object {"$($a).) $($_)"; $a++} | out-file -FilePath  $LogFolder\successUsers.log -Force -Verbose
$su=(Get-Content "$LogFolder\successUsers.log").count
$fu=(Get-Content "$LogFolder\FailedUsers.log").count
Write-Host "$fu Users Creation Failed and " -NoNewline -ForegroundColor red
Write-Host "$su Users Successfully Created "  -NoNewline -ForegroundColor green
Write-Host "--> Emailing LogsFolder have a Look and review." -ForegroundColor Magenta

The parseexact() method allows you to specify what format the date is in. You are very close. Simply change it to

[datetime]::ParseExact($_.wdate,'yyyy-MM-dd',$null)

If the date looked like 20200702 then you would’ve had the correct format.

Ok I was close…thank you for the reply. I am still unsure on how to define that variable. Do I still do a $Disable = $User.Wdate? Further in the script I need to add 30 days to the date in the csv and as of right now that isn’t working. Is there another way I need to define this variable to use it in the script?

Once you have an object of [DateTime] type, then you can use AddDays() method to add days.

$r = Get-Date
$r.AddDays(30)