2

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?

guntbert
  • 553
  • 7
  • 21
nate
  • 407
  • 5
  • 7
  • 17

3 Answers3

9

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.

Trevor Hickey
  • 203
  • 1
  • 2
  • 6
HopelessN00b
  • 53,385
  • 32
  • 133
  • 208
2

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
{
  param([string]$computer)

    [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
}
masegaloeh
  • 17,978
  • 9
  • 56
  • 104
0

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