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 

Answers


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

Need Your Help

UIView with uiindicator sethidden = No not working after sethidden = YES

ios iphone objective-c uiview uistoryboard

I have a uiView covering the screen with a label and uiindicator that runs when i am doing a network call.

Issue in running generated build using Selenium Webdriver?

java eclipse netbeans selenium-webdriver

I have prepared a testing framework using selenium webdriver.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.