We can easily find difference between two dates with powershell. We can get difference in Days, Hours, Minutes and Seconds.
Total Days between two dates
The below powershell script show the total number of days between two dates.
$FromDate =[DateTime] "04/20/2015" $ToDate =[DateTime] "07/07/2015" ($ToDate - $FromDate).TotalDays
Total Hours between two dates
The below powershell script show the total number of hours between two times.
$FromDate =[DateTime] "07/05/2015" $ToDate =[DateTime] "07/07/2015" ($ToDate - $FromDate).TotalHours
Total Minutes between two times
$FromDate = [DateTime] "07/07/2015 1:47:31 PM" $ToDate = [DateTime] "07/07/2015 3:47:31 PM" ($ToDate - $FromDate).TotalMinutes
Advertisement
Hi everyone , I am using the following code to display some values , the issue I am having is , the subtraction between two dates as in the expression [datetime]::Now.AddDays(-365) – $_.PasswordLastSet).Days
is evaluating to null in my data, what could be the reason, how to fix this code ?
Get-ADUser -Filter * -Properties PasswordLastSet, Mail -SearchBase $searchBase |
Where-Object{
([datetime]::Now.AddDays(-365) – $_.PasswordLastSet).Days -in @(” “,364, 350, 354, 340, 335,’ ‘,”,””)
}|
Select-Object name, PasswordLastSet,@{n=’DaysUntilExpired’;e={(([datetime]::Now.AddDays(365) – [datetime]::$_.PasswordLastSet).Day).days}} |
Add-Content -Path C:\Temp\PasswordExpList.txt
You might have received this problem with the null value in PasswordLastSet property. You can filter users who have PasswordLastSet value not null before applying this logic.
Ex: Get-ADUser -Filter * -Properties PasswordLastSet, Mail -SearchBase $searchBase |
Where-Object { $_.PasswordLastSet -ne $null}