Use PS to retrieve a list of computers

This topic contains 5 replies, has 6 voices, and was last updated by Profile photo of The IT guy The IT guy 1 year, 3 months ago.

  • Author
  • #26847
    Profile photo of Lars


    I need a PS script to retrieve a list of computers (from AD) and the user last logged on to every PC.

    Is that possible with PS?



  • #26852
    Profile photo of Matt Bloomfield
    Matt Bloomfield

    In a roundabout way, yes it is possible. It's not as straightforward as pulling the information from SCCM so if you have that, that's the easier option, as the information is already in the SCCM database.

    To achieve something similar in PowerShell, you have two tasks. First you'll need to generate the list of computers. You can do this with the Active Directory module and Get-ADComputer. The Active Directory module will be on your Domain Controller or, ideally, you'll be using it on a computer that has RSAT installed.

    You should use Get-Help to figure out exactly what you need to do, but a basic list of computers could be produced with

    Get-ADComputer -filter * | Select-Object -ExpandProperty Name | Out-File C:\computers.txt -append -noclobber

    Now that you have a list of all the computers in your Active Directory you can use a WMI query to find out who is logged on. The current logged in user can be found in the username property of Win32_ComputerSystem.

    You can use foreach to loop over all the computers in the computers.txt file and run

    Get-WMIObject Win32_computersystem -computerName $computer | Select-Object UserName 

    I hope I've given you enough information to make a start on a script.

  • #26853
    Profile photo of Richard Diphoorn
    Richard Diphoorn

    What you are asking is quite troublesome to accomplish. This is because you need to enable security auditing on the system.

    Don Jones also pointed this out in the following forum comment: LINK

    Maybe that forum topic gives you some pointers to look for.

  • #26888
    Profile photo of Tim Pringle
    Tim Pringle

    Hi Lars,

    Agree with Matt., a task like that is really better suited to a product such as SCCM. It can retrieve the information for you immediately.

    From an inventory point of view, Active Directory discovery is one of the main mechanisms SCCM can use for identifying computers, and getting the details of a WMI class is easily done via the hardware inventory.

    If you just need it for finding out who is logging onto a computer, then a logon script that writes the information to a text file is maybe an idea.

  • #26889
    Profile photo of kvprasoon

    Hi Team-Hopes The bellow Code will meet the requirements.

    Get-AdComputer -Filter * -Properties Description|Select -Expand Name,Description|Export-Csv  
    Get-AdComputer -Filter * -Properties Description|Select -Expand Name,Description|Convertto-Html|Out-File  

    Kindly Correct If anyMistakes...


  • #27210
    Profile photo of The IT guy
    The IT guy

    Hi Lars, Here is a snippet to get the last logged on user

You must be logged in to reply to this topic.