Powershell script to generate a report of all computers in OU

Posted on 2014-10-22
1 Endorsement
Last Modified: 2014-10-29
I need to create a PowerShell script which will generate a report for me of all computers in OU.

Report needs to be formatted as html or excel file and should look like this:

Name                OperatingSystem                                     Description
---------                --------------------------                                   -----------------
Server2             Windows Server 2008 R2 Standard     File Server
Lync13Srv        Windows Server 2008 R2 Standard      Lync 2013 Server
Server20          Windows Server 2012 R2 Standard      Standalone Hyper-V Server

Note: Report must have 'Description' field
Thanks in advance
Question by:Olevo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 81

Expert Comment

by:David Johnson, CD, MVP
ID: 40398469
something along this line will work

$computers = Get-ADComputer -LDAPFilter "(name=*)" -SearchBase "CN=Computers,DC=Fabrikam,DC=com"
foreach ($computer in $computers)
Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer | Select-Object -Property * | select PSComputername, Name ,Description | export-csv c:\example.csv 

Open in new window


Author Comment

ID: 40398508
Thanks David, my PowerShell skills is not up to the speed just yet. Please correct me if I wrong. First you get the list of all computers (Get-ADComputer) in OU and then you‘re querying (connecting to) computer with WMI command (Get-WmiObject) to get additional info. I’m I right? If this the case we will have problem getting info from the computers which are not online (switched off). All I want is to get info from Domain Controller… Perhaps, we can get ‘Description’ info from ‘Attribute Editor’
Attribute Editor

Expert Comment

by:Mai Ali (MVP)
ID: 40398990
For script that list all computers on ou, you can check below link
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 16

Expert Comment

by:Joshua Grantom
ID: 40399714
Here is a 1 liner

Get-ADComputer -filter * -SearchBase "OU=Servers,DC=Fabrikam,DC=com" -properties Name,OperatingSystem,Description | Select Name,OperatingSystem,Description | Export-CSV C:\ServerInfo.csv -nti

Open in new window


Author Comment

ID: 40400663
Thanks Joshua, that is exactly what I want. May I trouble you for an additional request? How hard it would be to make something like this: Our internal web site will have a page with a link, when I click on it, code within a web page (PowerShell command above)  will generate Servers information (Name,OS, Description, etc.) as a nice formatted html table.
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40400666
What is your internal website hosted with?

Author Comment

ID: 40400812
Thanks Joshua, This "Company Servers information" web page will be running on IIS 8.5 (Windows Server 2012 R2). Basically I need this page for my manager, so he can see list (click on a link) all our servers and what they do (Description) in our AD domain any time he wants. I'm guessing that it would be much easier just to run PowerShell script as a schedule task instead of fulling around with the web development?! Anyway, what do you think how I should accomplish my goal?
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40400847
Tomorrow I will modify the powershell script to output to an HTML document, then you can just have an HTML link to the output file.

Then you can run the powershell as a scheduled task and it will update the html file on whatever schedule you want. Simple as that.
LVL 16

Accepted Solution

Joshua Grantom earned 500 total points
ID: 40402199
Here you go! This has basic styling but you can modify that how you like if you want to add color or adjust spacing.

#Page Title
$title = "Server Information"
#HEAD Section (CSS for Styling)
$head = "<style> BODY{background-color:#FFFFFF;} TABLE{width: 75%; border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;} TH{border-width: 1px;padding: 10px;border-style: solid;border-color: black;background-color:#999999} TD{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:#FFFFFF} </style>"
#Page Header
$body = "<H2>Information about Servers in $search</H2>"
#Path to save html report
$path2output = "C:\ServerInfo.html"
#OU to query for report
$search = "OU=Servers,DC=DOMAIN,DC=com"

Get-ADComputer -filter * -SearchBase $search -properties Name,OperatingSystem,Description | ConvertTo-Html Name,OperatingSystem,Description -title $title -head $head -body $body | Set-Content $path2output

Open in new window


Author Comment

ID: 40412472
Thanks Joshua that is exactly what I want

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
This article demonstrates probably the easiest way to configure domain-wide tier isolation within Active Directory. If you do not know tier isolation read…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question