# Parsing date from CSV

**URL:** <https://forums.powershell.org/t/parsing-date-from-csv/14948>\
**Category:** PowerShell Help\
**Created:** [September 15, 2020, 2:38am UTC](https://forums.powershell.org/t/parsing-date-from-csv/14948 "2020-09-15T02:38:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chm0d](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chm0d/32/34_2.png) [@chm0d](https://forums.powershell.org/u/chm0d)\
**Post date:** [September 15, 2020, 2:38am UTC](https://forums.powershell.org/t/parsing-date-from-csv/14948/1 "2020-09-15T02:38:38Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [September 15, 2020, 6:00am UTC](https://forums.powershell.org/t/parsing-date-from-csv/14948/2 "2020-09-15T06:00:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chm0d](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chm0d/32/34_2.png) [@chm0d](https://forums.powershell.org/u/chm0d)\
**Post date:** [September 15, 2020, 6:39am UTC](https://forums.powershell.org/t/parsing-date-from-csv/14948/3 "2020-09-15T06:39:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [September 15, 2020, 8:07am UTC](https://forums.powershell.org/t/parsing-date-from-csv/14948/4 "2020-09-15T08:07:50Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/parsing-date-from-csv/14948/5 "2024-05-16T20:28:55Z")

</div>


