# Trim or Modify Output

• ### Question

• I'd like to compare two dates in order to find out if a group has been changed, but I need the format to match.

$Groups = Import-Csv 'D:\path\to\csv' -Header "DLGroups"$today = Get-Date -Format MM/dd/yyyy

foreach ($g in$Groups)

{

$DLGroups =$g.DLGroups

$status = Get-DistributionGroup -Identity$DLGroups | select WhenChanged

if ($status.WhenChanged -eq$today)

{

Sends an email

}

}

-------------

$today output is like this: 02/22/2018$status output is like this: 02/20/2018 11:13:30 AM

Either I need to edit the output of $status (to drop the time) or possibly create a new variable and modify it within? Something like this (expect something that works this does not):$edit = $status.(get-date -Format MM/dd/yyyy) Many thanks. Thursday, February 22, 2018 3:23 PM ### Answers •$status.whenChanged.Date -ge [DateTime]::Today

"Today" only matches midnight.

Yesterday only:

$status.LastWriteTime -ge [datetime]::Today.AddDays(-1) -and$status.LastWriteTime -lt [datetime]::Today

\_(ツ)_/

• Edited by Thursday, February 22, 2018 5:09 PM
• Marked as answer by Friday, February 23, 2018 12:47 PM
Thursday, February 22, 2018 5:09 PM

### All replies

• Let's clear up a misunderstanding first. In PowerShell, dates are DateTime objects. DateTime objects are not strings and do not have a "format" unless you output them as strings.

If you want to find out if an object's whenChanged is today, you would use:

if ( $status.whenChanged.Date -eq [DateTime]::Today ) { ... }  The whenChanged attribute is a DateTime object. The Date property gets today's date. The idea here is not to convert them to strings and then compare. The whole point is to take advantage of the fact that DateTime is an object and you do not have to do string parsing or comparisons. -- Bill Stewart [Bill_Stewart] • Proposed as answer by Thursday, February 22, 2018 4:09 PM Thursday, February 22, 2018 3:49 PM •$status.whenChanged.Date -ge [DateTime]::Today

"Today" only matches midnight.

Yesterday only:

$status.LastWriteTime -ge [datetime]::Today.AddDays(-1) -and$status.LastWriteTime -lt [datetime]::Today

\_(ツ)_/

• Edited by Thursday, February 22, 2018 5:09 PM
• Marked as answer by Friday, February 23, 2018 12:47 PM
Thursday, February 22, 2018 5:09 PM
• Once I changed "lastwritetime" to "whenchanged" it worked fine. Many thanks, but if I understand this correctly this script will need to run every day in order to find if a change occurred since it's looking at "yesterday". Correct?

One more question regarding the datetime, could this same thing be accomplished with easier to read code with get-date? Thanks again.

Friday, February 23, 2018 12:53 PM
• Get-date is not easier to read and does not have a direct way of returning a normalized zero day.  The net framework object is what Get-Date returns.

$d = Get-Date$d.GetType()

You need to start your scripting by actually learning PowerShell.  Guessing will just waste everyone's time.

\_(ツ)_/

Friday, February 23, 2018 1:13 PM
• Get-Date (without parameters) is functionally equivalent to [DateTime]::Now (i.e., it outputs a DateTime object containing the current date and time). Likewise this:

(Get-Date).Date

Is equivalent to this:

[DateTime]::Today

and this:

([DateTime]::Now).Date

You don't have to write a script to experiment with these. Just open a PowerShell console and explore.

-- Bill Stewart [Bill_Stewart]

Friday, February 23, 2018 2:51 PM
• Excellent, thanks!
Friday, February 23, 2018 3:08 PM