Custom Formatting with PowerShell v2.0


Hi all,

I'm trying to format the output of table I created based on custom objects created like this

$objFile = New-Object psobject
$objFile | Add-Member noteproperty Name $name
$objFile | Add-Member noteproperty fSize $Size
$objFile | Add-Member noteproperty Extention $ext

So I fill up the table and try to output using this simple formatting:

$theFiles | Ft @{Expression={$_.Name};Label="Name";width=25},@{Expression={$_.fSize};Label="Size";width=25}

This always gives me the error "
out-lineoutput : The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not in the correct sequence. This is likely caused by a user-specified "format-table" command which is conflicting with
the default formatting."

When I use  format-table in combination with the field names it works as expected (    $theFiles | ft name,fsize   )


LVL 4
Bart-VandyckAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:

How are you building $theFiles? Is it just a collection of $objFile objects?

Chris
0
Chris DentPowerShell DeveloperCommented:

For instance... I can build the object like this and run it without error.

Which version of PowerShell are you using? All my testing is done under PowerShell 2 which occasionally throws a spanner in the works if trying to compare with 1.

Chris
$theFiles = 1, 2, 3 | %{
  $name = "Name$_"
  $Size = "Size$_"
  $Ext = "Ext$_"

  $objFile = New-Object psobject
  $objFile | Add-Member noteproperty Name $name
  $objFile | Add-Member noteproperty fSize $Size
  $objFile | Add-Member noteproperty Extention $ext

  $objFile
}

$theFiles | Ft @{Expression={$_.Name};Label="Name";width=25},@{Expression={$_.fSize};Label="Size";width=25}

Open in new window

0
Bart-VandyckAuthor Commented:

Chris,

You're example works for me to;-)

Well the code looked like this

function MakeObj($name,$size,$ext)
 
{
$objFile = New-Object psobject
$objFile | Add-Member noteproperty Name $name
$objFile | Add-Member noteproperty fSize $Size
$objFile | Add-Member noteproperty Extention $ext
$objFile
 
}


$colItems = (Get-ChildItem $startFolder -Recurse | Measure-Object -property length -sum -ErrorAction 0)
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"
$colItems = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems)
    {
        $subFolderItems = (Get-ChildItem $i.FullName -ea 0 -recurse | Measure-Object -property length -sum -ea 0)
        Write-Progress -Activity "Searching for folders larger then $FolderSize MB" -CurrentOperation $i.FullName -Status "Checking:"
            if (($subfolderitems.sum/1MB) -gt $folderSize)
            {
                  makeobj $i.FullName ($subFolderItems.sum / 1MB) $i.Extension
                  $theFiles = $theFiles + (makeobj $i.FullName $subFolderItems.sum $i.Extension)
            }
           
    }
 
 $theFiles = $theFiles | Sort-Object -Property Size -Descending  
 $theFiles | ft @{Expression={$_.Name};Label="Name";width=25},@{Expression={$_.fSize};Label="Size";width=25}


So this gave me the problem. Now when I was looking it over this morning, i saw I did create the psobject twice but only added the last one

                  makeobj $i.FullName ($subFolderItems.sum / 1MB) $i.Extension
                  $theFiles = $theFiles + (makeobj $i.FullName $subFolderItems.sum $i.Extension)

The first line is/was actually a left over from the beginning of the script, but as it turns out if I delete this first line it starts working. So deleting "makeobj $i.FullName ($subFolderItems.sum / 1MB) $i.Extension" and the out-put of $theFiles starts working, which I found a bit strange. Do you have an explanation for this?

A quick other question, how to you get the syntax highlighted  code in the message?

rgds

Bart

0
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

Chris DentPowerShell DeveloperCommented:

This one is quick and easy:

> A quick other question, how to you get the syntax highlighted  code in the message?

The Code link at the bottom of the comment box will pop up a code box. It won't insert in-line at the moment, that functionality will be added eventually.

Chris
0
Chris DentPowerShell DeveloperCommented:

> Do you have an explanation for this?

I'm afraid I don't, I'm still unable to reproduce the error you receive. This could be a version disparity, which version of PowerShell are you running this in?

I'm a little confused about the output from the script. It returns folders over a certain size? Why include Extension?

Either way, it can be simplified, no need to construct your own object, after all, taking an existing object and writing it to a new object is something Select-Object was made for.

You can happily apply the Format-Table expression to any of these, you only need modify the property it accesses for Name (FullName vs Name).

Chris
# If you want sub-folders over a certain size

$FolderSize = 1
Get-ChildItem | ?{ $_.PsIsContainer } | `
  Select-Object FullName,
    @{n='Size';e={ (Get-ChildItem $_.FullName -Recurse | Measure-Object Length -Sum).Sum/1Mb }} | `
  ?{ $_.Size -gt $FolderSize } | Sort-Object Size -Descending

# If you want files over a certain size

$FolderSize = 1
Get-ChildItem -Recurse | `
  Select-Object FullName, Extension, @{n='Size';e={ ($_.Length)/1Mb }} | `
  ?{ $_.Size -gt $FolderSize } | Sort-Object Size -Descending

# If you want sub-folders over a certain size, recursive

Get-ChildItem -Recurse | ?{ $_.PsIsContainer } | `
  Select-Object FullName,
    @{n='Size';e={ (Get-ChildItem $_.FullName -Recurse | Measure-Object Length -Sum).Sum/1Mb }} | `
  ?{ $_.Size -gt $FolderSize } | Sort-Object Size -Descending

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bart-VandyckAuthor Commented:

Hi Chris,

I can understand the confusion about the output, let's just say I was having fun playing, learning PowerShell. The whole thing started as my USB stick was almost full and I wanted to look the PowerShell way for it. The goal of the script is to 1. search all folders above a certain size. 2. Display a menu 3. when you type in the number of the folder you want to see, have a grouped view of the files based on the extention.

Thanks for you're examples on Select-object, I have never used it myself, but indeed it will simplify the whole thing and you're example make the use of it verry clear.

Thanks a lot!!

Bart
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.