Generate ad and folder data, match and compare then send report in mail body


I want to create a power shell script that will do:

  • Generate data from active directory (when did the computer last connect to the domain
  • When was the backup location with the name of the computer last modified
  • Match the outputs with "name" to one output
  • Compare just the date part for the output for example for computer "X" whenChanged "29.12.2020 20:25:22" and for computer "x" LastWriteTimeUtc "29.12.2020 17:20:26" is a match
  • send report via email properly formatted (in a table for example) with all matched and mismatched data in the body of the mail. If matched "backup successful" if not "check backup".
  1. Generate data
I have this done and I like the output of the commands. If you have any suggestions how to improve this let me know:


Get-ADComputer -Filter * -Properties * | Select-Object Name, whenChanged | Export-Csv -Path c:\last-connect.csv -NoTypeInformation

Sample output of last-connect.csv (names changed for privacy)

“X”,“29.12.2020 20:25:22”
“z”,“29.12.2020 2:00:55”
“a-L1”,“23.12.2020 10:46:39”
“s-D1”,“21.12.2020 9:18:48”
“C2-D1”,“23.12.2020 13:10:46”
“j-L1”,“30.4.2020 15:20:35”
“C2-L1”,“14.7.2016 10:54:48”
“g-L1”,“24.8.2020 8:52:28”
“J-L1”,“23.8.2019 19:29:17”
“U-D1”,“15.6.2017 10:33:31”
“E-D1”,“28.12.2020 9:38:26”
“Q-L1”,“29.10.2016 22:30:19”
“b-L1”,“12.10.2020 7:50:51”
“C2-L2”,“22.10.2018 12:53:04”
“J-L2”,“20.12.2020 22:00:15”
“P-L1”,“15.2.2018 13:24:28”
“t-L1”,“23.12.2020 8:48:41”
“K-D1”,“28.12.2020 19:57:53”
“R-D1”,“27.12.2020 8:39:02”
“v-L1”,“29.12.2020 13:18:57”
“z10”,“12.10.2020 7:54:22”
“Z11”,“11.12.2020 14:00:16”
“Z12”,“21.12.2020 7:54:44”
“y-L1”,“15.10.2020 7:54:54”
“A1”,“29.12.2020 7:51:20”
“I-L2”,“24.12.2020 11:29:45”


Get-ChildItem \backuplocation | Where{$_.LastWriteTime} | Select-Object Name, LastWriteTimeUtc | Export-Csv -Path c:\lastwrite.csv -NoTypeInformation

Sample output of lastwrite.csv (names changed for privacy)

“C-D1”,“30.5.2019 15:00:24”
“C2-d1”,“29.12.2020 11:42:39”
“C-D1_1”,“3.3.2020 12:04:54”
“t-l1”,“11.12.2020 12:52:57”
“s-D1_1”,“24.12.2020 10:05:37”
“j-l1”,“11.10.2019 9:08:38”
“R D1”,“11.9.2019 10:02:47”
“K-D1”,“29.12.2020 11:36:43”
“j-l1”,“28.12.2020 12:08:04”
“R-d1”,“10.9.2019 9:39:40”
“s-D1”,“21.5.2019 15:01:22”
“a1”,“29.12.2020 15:32:40”
“x”,“29.12.2020 17:20:26”
“b-l1”,“16.10.2020 9:33:15”
“g-l1”,“2.9.2020 9:41:29”
“a-l1”,“23.12.2020 10:40:40”
“d-d1”,“18.3.2019 14:15:17”
“K-l1”,“22.8.2019 5:26:08”
“v-L1”,“29.12.2020 12:54:58”

I have a few challenges with this. As you can see in one output the names are uppercase in other are lower case. I want to match even if match case do not match. Also how do you compare the date without the time?

Any suggestion how to do this. I am beginner at power shell. Give me suggestions how to do this with minimal sample code so I can learn from this. Sending email won’t be a problem once I get the report. I can figure that on my own just how to write the logic and how to handle the data I am most interested in hearing from you. Thank you!


All of the things you want to do are possible. First start with storing the data in variables rather than exporting it:

$computers = Get-ADComputer -Filter * -Properties * | 
             Select-Object Name, whenChanged

$backups = Get-ChildItem \\backuplocation | 
           Where{$_.LastWriteTime} | 
           Select-Object Name, LastWriteTimeUtc

The next step is joining the objects, if you search for “Powershell Join Objects” you should find examples (you do not need the large functions unless you are trying to do a specialized join). Recommend you get a book like Learn Powershell in Month of Lunches.

As far as comparison, most comparison operators are not case-sensitive unless you specifically use a case-sensitive operator (e.g. -ceq):

#standard equals comparison
PS C:\Users\rasim> 'bye 2020' -eq 'BYE 2020'

#case-sensitive equals comparison
PS C:\Users\rasim> 'bye 2020' -ceq 'BYE 2020'

#case-sensitive equals comparison
PS C:\Users\rasim> 'BYE 2020' -ceq 'BYE 2020'


Take a look at Send-MailMessage to send the email when you have the other components working