I am looking for a script to show all the machines, hostnames, and OS versions on my domain. I have found a few scripts, but none of the scripts that I have found will do both.

Here is an example of one that I have found:

$strCategory = "computer"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = ("(objectCategory=$strCategory)")
$colProplist = "name"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
  $colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
  {$objComputer = $objResult.Properties; $objComputer.name}

Can someone please tell me how to create a script for powershell to list the hostname and operating system version?


To get OS Version:

 Get-ADComputer -Filter * -Property * | Format-Table Name,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion -Wrap –Auto

Get-ADComputer returns the computer name by default, as well.

  • I guess I didn't look in the right place. Thank you for you help
    – nate
    Mar 28 '14 at 19:57

Not sure if this is exactly what you are looking for, but it works for me. You need to know the computer name which you pass into the function. And it will give you the OS in the return string. I'm sure it could be modified to suit other needs.

function global:Get-OSInfo

    [string] $strReturn = '' 

    if([string]::IsNullOrEmpty($computer) -eq $false)
        # Create the connection to Active directory.
        $dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
        $root = $dom.GetDirectoryEntry()
        $search = [System.DirectoryServices.DirectorySearcher]$root

        # Search the directory for our user.
        $search.Filter = "(&(objectclass=Computer)(sAMAccountName=$computer$))"
        $result = $search.FindOne()

        if ($result -ne $null)
            $system = $result.GetDirectoryEntry()
            $strReturn = $system.Properties["operatingSystem"]

    return $strReturn

I realise this is an old thread and doesn't use powershell, but why not running ADExplorer.exe, which is part of SysInternals Suite?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.