Get-ADComputer -filter question RRS feed

  • Question

  • I have a csv of 300+ usernames and need to find out what computers they have used in AD. In the description field, we list the username of the last person to log on to that machine. I'd like to use powershell to query AD and pull those machine names with user name into a new csv. Ultimately, i want a single list of usernames and computer names but i'm not having much luck with the -filter.

    Any help would be appreciated!

    Friday, October 20, 2017 8:09 PM

All replies

  • A script that updates the computer object every time a user logs on, or even every time a different person log onto a computer, will generate a lot of replication traffic. A better scheme might be a logon script that appends the date, time, user name, and computer name to a shared log file (comma delimited). the resulting log file can be opened in Excel, where the lines can be sorted by user an date. The logon script can be as simple as the following batch file:

    @echo off
    echo %date% %time%,%UserName%,%ComputerName% >> \\MyServer\MyShare\LogUsers.log

    If you want to use your scheme, the following snippet, showing how to query for a computer with a given value assigned to description, may help:

    $Computers = Import-Csv -Path "Computers.csv"
    ForEach ($Computer In $Computers)
        $User = $Computer.UserName
        $Computers = Get-ADComputer -Filter {Description -eq $User}
        # Output to CSV.

    $Computers will be a collection of objects, with properties such as Name, SamAccountName, and DNSHostName. Note that Name is the Relative Distinguished Name of the computer, which does not necessarily uniquely identify the object in AD.

    One reason we hesitate to offer code is that we do not know how your attempts have failed. I have assumed you cannot query for the computers. I also assumed your CSV has a header line defining the field UserName. But you might have had trouble outputting results to a CSV. It would help to provide the code you have so far, so we can see what is wrong.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Friday, October 20, 2017 8:56 PM