Directory list with Format-Table

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of Jakko Valgi Jakko Valgi 1 year, 10 months ago.

  • Author
    Posts
  • #25217
    Profile photo of Jakko Valgi
    Jakko Valgi
    Participant

    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 🙂

    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 
  • #25218
    Profile photo of Jakko Valgi
    Jakko Valgi
    Participant

    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 🙂

    Thank you in advance guys! 🙂

    -Jakko

  • #25225
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    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.

  • #25226
    Profile photo of Craig Duff
    Craig Duff
    Participant
    $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.

  • #25227
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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
    
  • #25229
    Profile photo of Jakko Valgi
    Jakko Valgi
    Participant

    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 🙂

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

You must be logged in to reply to this topic.