user1342164
user1342164

Reputation: 1454

Powershell to compare output with a CSV file?

I am using the code below to get a list of computer names where they were last modified 181 days ago. I have a .csv file with 2 columns (ComputerName, UserName) Is there a way I can match the output from the code below with the computername column in the csv file and show the output/matches?

$Days = (Get-Date).AddDays(-181) 

Get-ADComputer -Property Name,lastLogonDate -Filter  {lastLogonDate -lt $Days} -Server DomainController -Searchbase "OU=US,DC=contoso,DC=net" | FT Name,lastLogonDate 

Upvotes: 0

Views: 353

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

Change ... | FT Name,lastLogonDate to ... | select Name,lastLogonDate. You can still pipe the result into ft to format it as a table, but separating selection from presentation will make it easier to put in additional filters.

For displaying just the computers that have matches in your CSV you could do the following:

$computers = Import-Csv 'your.csv' | % { $_.ComputerName }

Get-ADComputer -Property Name,lastLogonDate ... | select Name,lastLogonDate |
    ? { $computers -contains $_.Name } | ft

To include the username from the CSV with the result you could do something like this:

$computers = @{}
Import-Csv 'your.csv' | % { $computers[$_.ComputerName] = $_.UserName }

Get-ADComputer -Property Name,lastLogonDate ... |
    ? { $computers.Keys -contains $_.Name } |
    select Name,lastLogonDate,@{n='Username';e={$computers[$_.Name]}} | ft

Upvotes: 1

Related Questions