Directory list with Format-Table

Hey,

So, I’m trying to get directory list with format-table.
Here is what I got and it’s not doing exactly what I need.

$rootdir = "\\somenetworkpath\folder"

$directories =  @("\somefolder1\TMP",`
			 "\somefolder2\Diff",`
             "\somefolder3\Diff",`
             "\somefolder4\Diff",`
             "\somefolder5\Diff",`
             "\somefolder6",`
	     "\somefolder7",`
	     "\somefolder8\differentdiff",`
             "\somefolder9\Diff"
             "end")

$i = 0
do {
    $temp1 = $rootdir + $directories[$i]
    $paths = Get-ChildItem -path $temp1 | Sort-Object CreationTime -Descending | Select-Object -First 1
    Write-Host $paths
    $i++
} while ($directories[$i] -ne "end")

now this is running just fine and does exactly what it’s supposed to do but I want to see more details, so if I add

 | Format-Table -Property CreationTime, LastWriteTime, FullName, Length 

after

 Select-Object -First 1 

then the result is “Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData”.
Which is not exactly whatI want :slight_smile:

This works just fine if I do

 Get-ChildItem -Path \\somenetworkpath\path\to\where\ever | Sort-Object CreationTime -Descending | Select-Object -First 1 | Format-Table -Property CreationTime, LastWriteTime, FullName, Length 

Hit that submit button too soon.

So the question is, how to make it work in the first example that I gave? It should be possible, I think :slight_smile:

Thank you in advance guys! :slight_smile:

-Jakko

You’re passing formatted data (Format-Table) to Write-Host, which won’t work. For example, this will work:

PS C:\Windows\system32> $paths = Get-ChildItem -path 'c:\scripts' | Sort-Object CreationTime -Descending | Select-Object -First 1 | Format-Table -Property CreationTime, LastWriteTime, FullName, Length

PS C:\Windows\system32> $paths

CreationTime                      LastWriteTime                     FullName                                                   Length
------------                      -------------                     --------                                                   ------
5/12/2015 7:28:54 PM              5/12/2015 7:48:32 PM              C:\scripts\cmerrlogBuild.csv                              8914923

But if you pass it to write host:

PS C:\Windows\system32> Write-Host $paths
Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.P
owerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Com
mands.Internal.Format.FormatEndData

I’d recommend creating a PSCustomObject instead of formatting the data. It’s a lot more flexible. Or better yet, build out your script as a function without limiting the data being passed and use Select-Object in the pipe naturally. But if you’re looking for a report kind of script, then PSCustomObject is probably a better way to go.

$rootdir = "\\somenetworkpath\folder"
 
$directories =  @(
    "\somefolder1\TMP"
    "\somefolder2\Diff"
    "\somefolder3\Diff"
    "\somefolder4\Diff"
    "\somefolder5\Diff"
    "\somefolder6"
    "\somefolder7"
    "\somefolder8\differentdiff"
    "\somefolder9\Diff"
)
 
ForEach($directory in $directories) {
    Get-ChildItem -Path (Join-Path $rootdir $directory) |
    Sort-Object CreationTime -Descending |
    Select-Object -First 1 |
    Select-Object CreationTime, LastWriteTime, FullName, Length  
}

Format-Table turns the file system objects into a formatting object, which if you try to use Write-Host on converts to a very unhelpful string. If you replace Format-Table with Select-Object and then just let it fall out off the pipeline without catching it in a variable, you should get what you want. I also powershellerized the loop.

Your logic is a bit messed up. In your post you don’t show what you are running the Format-Table against, but you are overwriting it each time. There are two methods of creating an object with all results, I like assigning the results to the for loop and the other appends data as it goes (currently remarked out):

$rootdir = "C:\Windows"
 
$directories =  @("\Diagnostics",
			      "\Debug",
                  "\Fonts")

$results = foreach ($directory in $directories) {
    Get-ChildItem -path ("{0}{1}" -f $rootdir, $directory) | Sort-Object CreationTime -Descending | Select -First 1
}

#$results = @() #Create a blank object (array) to add items to
#foreach ($directory in $directories) {
    #Add each result to the blank object (array)
#    $results += Get-ChildItem -path ("{0}{1}" -f $rootdir, $directory) | Sort-Object CreationTime -Descending | Select -First 1
#}

$results | Format-Table -Property CreationTime, LastWriteTime, FullName, Length

Thank you guys!

Craig, yeah, true the “select-object” works, but the table is messed up like that, well, I get the data out at-least :slight_smile:

Rob, your method works just like I need, thank you so much!