0

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 

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, the code is working but how do I show the username column from the csv next to the computername? I am not sure how to do that ft Name, Username?
What does this line do? % { $_.Computer_Name }
That's a ForEach-Object loop, which creates a list (array) of computer names. The modified version uses a hashtable instead, with the computer names as keys and the user names as values.
For some reason the Employee_Name column in the output is only displaying {} on each row. Any idea why its not returning the employee name?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.