# Custom object to html report

**URL:** <https://forums.powershell.org/t/custom-object-to-html-report/5684>\
**Category:** PowerShell Help\
**Created:** [February 3, 2016, 7:10pm UTC](https://forums.powershell.org/t/custom-object-to-html-report/5684 "2016-02-03T19:10:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![martijn-van-der-graa](https://avatars.discourse-cdn.com/v4/letter/m/ac91a4/32.png) [@martijn-van-der-graa](https://forums.powershell.org/u/martijn-van-der-graa)\
**Post date:** [February 3, 2016, 7:10pm UTC](https://forums.powershell.org/t/custom-object-to-html-report/5684/1 "2016-02-03T19:10:31Z")

</div>

Hi,

I’m creating a script that iterates over some folders and does a command git status.

The result is added to a custom object. See code:

$files = Get-ChildItem;  
echo $files;

$resultArray = @();

for ($i=0; $i -lt $files.Count;$i++) {

```
cd $files[$i].FullName;
echo In: $files[$i].FullName;
$cmdResult = New-Object System.object;
$gitResponse = git status | Out-String;

$cmdResult | Add-Member -type NoteProperty -name Name -Value $files[$i].FullName;
$cmdResult | Add-Member -type NoteProperty -name GitStatus -Value $gitResponse;
$resultArray += $cmdResult;
cd ..

```

}

My question is how can I create an html of the $resultArray.

It must be simple to do but my skills on powershell are limited. (just started learning it)

Yours,

---

<div class="post-metadata">

**Author:** ![wei-yen-tan](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wei-yen-tan/32/91_2.png) [@wei-yen-tan](https://forums.powershell.org/u/wei-yen-tan)\
**Post date:** [February 3, 2016, 7:25pm UTC](https://forums.powershell.org/t/custom-object-to-html-report/5684/2 "2016-02-03T19:25:28Z")

</div>

You will have to pipe it through to an Convertto-HtML fragment… like this

```
$resultArray | ConvertTo-Html -Fragment
```

(of course you can add your other components…check the help on Convert-HTML parameters.)

Then once you have created all your fragments you would create another variable

```
$report = Convertto-HTML -Body "$ResultArray"
```

to out put to a file you would pipe it to out-file

```
$report | out-file -path c:\temp\report.html
```

In a nutshell you are creating a table out of the object (which is what what the parameter fragment does) and then converting that to HTML by using Convertto-HTML.

Hope this helps 🙂

EDIT Edited some errors in cmdlets

---

<div class="post-metadata">

**Author:** ![martijn-van-der-graa](https://avatars.discourse-cdn.com/v4/letter/m/ac91a4/32.png) [@martijn-van-der-graa](https://forums.powershell.org/u/martijn-van-der-graa)\
**Post date:** [February 3, 2016, 7:32pm UTC](https://forums.powershell.org/t/custom-object-to-html-report/5684/3 "2016-02-03T19:32:08Z")

</div>

I get.

Convert-HTML : The term ‘Convert-HTML’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and  
try again.

---

<div class="post-metadata">

**Author:** ![wei-yen-tan](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wei-yen-tan/32/91_2.png) [@wei-yen-tan](https://forums.powershell.org/u/wei-yen-tan)\
**Post date:** [February 3, 2016, 7:35pm UTC](https://forums.powershell.org/t/custom-object-to-html-report/5684/4 "2016-02-03T19:35:56Z")

</div>

Try Convertto-HTML

You can get the specific command if you check Get-Command \*HTML

---

<div class="post-metadata">

**Author:** ![martijn-van-der-graa](https://avatars.discourse-cdn.com/v4/letter/m/ac91a4/32.png) [@martijn-van-der-graa](https://forums.powershell.org/u/martijn-van-der-graa)\
**Post date:** [February 3, 2016, 8:27pm UTC](https://forums.powershell.org/t/custom-object-to-html-report/5684/5 "2016-02-03T20:27:20Z")

</div>

It works. Except the object is printed in the html file and not the attributes name and GitStatus

need to figure out how to do that.

---

<div class="post-metadata">

**Author:** ![wei-yen-tan](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wei-yen-tan/32/91_2.png) [@wei-yen-tan](https://forums.powershell.org/u/wei-yen-tan)\
**Post date:** [February 3, 2016, 9:00pm UTC](https://forums.powershell.org/t/custom-object-to-html-report/5684/6 "2016-02-03T21:00:22Z")

</div>

I don;t think I am following you… does the values appear?

if output the object as in

$ResultArray | Format-Table

what does it produce?

---

<div class="post-metadata">

**Author:** ![martijn-van-der-graa](https://avatars.discourse-cdn.com/v4/letter/m/ac91a4/32.png) [@martijn-van-der-graa](https://forums.powershell.org/u/martijn-van-der-graa)\
**Post date:** [February 3, 2016, 9:03pm UTC](https://forums.powershell.org/t/custom-object-to-html-report/5684/7 "2016-02-03T21:03:36Z")

</div>

Hi,

I get:

System.Object System.Object System.Object

My code is:

$files = Get-ChildItem;  
echo $files;

$resultArray = @();

for ($i=0; $i -lt $files.Count;$i++) {

```
cd $files[$i].FullName;
echo In: $files[$i].FullName;
$cmdResult = New-Object System.object;
$gitResponse = git status | Out-String;

$cmdResult | Add-Member -type NoteProperty -name Name -Value $files[$i].FullName;
$cmdResult | Add-Member -type NoteProperty -name GitStatus -Value $gitResponse;
$resultArray += $cmdResult;
cd ..

```

}  
$report = ConvertTo-HTML -Body “$ResultArray”;

$report | out-file -path c:\tmp\result.html

---

<div class="post-metadata">

**Author:** ![wei-yen-tan](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wei-yen-tan/32/91_2.png) [@wei-yen-tan](https://forums.powershell.org/u/wei-yen-tan)\
**Post date:** [February 3, 2016, 10:01pm UTC](https://forums.powershell.org/t/custom-object-to-html-report/5684/8 "2016-02-03T22:01:27Z")

</div>

Ok I think whats happening here is that your object is not being created properly.

When you create custom objects…your variables you have to populate them with proper objects not external commands.

The way I understand producing Custom Object is that you are basing it off other validated objects and then piecing it together.

So I am actually not sure $gitresponse would even produce a proper value because in that form powershell would not know how to interpret git status.

I don’t have git but the value git status would not produce objects. Its an external command and as such Powershell doesnt know how to deal with them.

Below is a short example of creating a custom object of three activedirectory groups and getting the counts.

```
#region Variables
$groupa = Get-ADGroup GroupA |Get-ADGroupMember 
$groupb = Get-ADGroup GroupB |Get-ADGroupMember
$groupc = Get-ADGroup GroupC |Get-ADGroupMember
#endregion
$AllGroupReport = @()
$AllGroupReport += New-Object psobject -Property @{
    GroupA = $groupa.count;
    GroupB = $groupb.count;
    GroupC = $groupc.count;
}
```

Why would this work? Because the values I chose for the variables is based off objects.  
The value of the variable $GroupA is actually Get-ADGroup GroupA |Get-ADGroupMember and this is not the same as listing text.

Once your values are based off objects then it will start to work.

I suggest that you work through a book like Powershell in a month of Lunches by Don Jones and watch his accompanying videos on Youtube

> **[Don Jones](https://www.youtube.com/user/powershelldon/videos)**
>
> Concentrated Technology's Don Jones presents PowerShell-related video tips, book companion content, and more!

Also I would strongly suggest that you go through the MVA training on Powershell.

> **[Getting Started with Microsoft PowerShell](https://learn.microsoft.com/en-us/shows/getting-started-with-microsoft-powershell/?l=r54irowy_2304984382)**
>
> Come learn Microsoft PowerShell from experts in the industry! Take this free course detailing how to make it work best for you.

If you want to jump start into objects I found some links on youtube that may help you.

[![](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/d/d9f2de1e94c13a66247dcbb4114b1663144d7447.jpeg "Chapter 19 - Custom Output Objects.mp4") ](https://www.youtube.com/watch?v=TLUzPvA0HUo)

Chapter 19 - Custom Output Objects.mp4 (Powershell in a month of Lunches)

and 04 | Objects for the Admin in PowerShell  
in Microsoft Virtual Academy

> **[Getting Started with Microsoft PowerShell](https://learn.microsoft.com/en-us/shows/getting-started-with-microsoft-powershell/?l=r54irowy_2304984382)**
>
> Come learn Microsoft PowerShell from experts in the industry! Take this free course detailing how to make it work best for you.

I hope this helps … 🙂

---

<div class="post-metadata">

**Author:** ![martijn-van-der-graa](https://avatars.discourse-cdn.com/v4/letter/m/ac91a4/32.png) [@martijn-van-der-graa](https://forums.powershell.org/u/martijn-van-der-graa)\
**Post date:** [February 3, 2016, 11:03pm UTC](https://forums.powershell.org/t/custom-object-to-html-report/5684/9 "2016-02-03T23:03:34Z")

</div>

Hi,

Thank you for all the help. Did see the training [https://mva.microsoft.com/en-us/training-courses/getting-started-with-powershell-3-0-jump-start-8276?l=r54IrOWy\_2304984382](https://mva.microsoft.com/en-us/training-courses/getting-started-with-powershell-3-0-jump-start-8276?l=r54IrOWy_2304984382) a while ago. That’s the reason I try to get this solved by using powershell.

I will just figure it, will be a good practice on powershell.

Yours,

---

<div class="post-metadata">

**Author:** ![wei-yen-tan](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wei-yen-tan/32/91_2.png) [@wei-yen-tan](https://forums.powershell.org/u/wei-yen-tan)\
**Post date:** [February 4, 2016, 5:40am UTC](https://forums.powershell.org/t/custom-object-to-html-report/5684/10 "2016-02-04T05:40:33Z")

</div>

No problem…One thing I suggest that you do is to pipe the cmdlet you want to use to Get-Member. Like the format:

Get-Childitem c:|Get-Member

See the input that it produces…If it produces a result like a standard cmdlet then it will work. If it just sits there, then most likely it is not a candidate for use in a custom object.

I am not quite sure if you have installed posh-git module…download from the powershell gallery install-module posh-git.

If you have check to see the object that it produces from the cmdlets you want to utilize from the github module using the example I showed above

If it produces any validated results then you are good to go.

Best of luck with your journey into Powershell…things start to make sense eventually. I am still learning as well

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:42pm UTC](https://forums.powershell.org/t/custom-object-to-html-report/5684/11 "2024-05-16T20:42:55Z")

</div>

Closed on May 16, 2024, 8:42pm UTC


