[PS] foreach Help - VBScript style example?

Discussion in 'Scripting' started by Andrew Watt [MVP], May 15, 2006.

  1. In light of the recent complaints about VBScript style in the Script
    Center examples I wonder if the same issue applies to this example in
    the foreach Help file:

    foreach ($file in Get-ChildItem)
    {
    if ($file.length -gt 100k)
    {
    Write-Host $file
    Write-Host $file.length
    Write-Host $file.lastaccesstime
    }
    }

    Wouldn't a "more PowerShell" way to write this be:

    get-childitem * |
    where-object {$_.Length -gt 100} |
    format-list Name, Length, LastAccessTime

    Andrew Watt MVP
     
    Andrew Watt [MVP], May 15, 2006
    #1
    1. Advertisements

  2. Andrew Watt [MVP]

    Mark Ayers Guest


    I think that is a great case where the command line nature of PowerShell
    trumps the script. Of course, we can have a line like the above in a script
    as well as the readable control structures. I think an appropriate example
    might show PSObject manipulation and the pipeline.

    I saw many scripts in the early days of our expsure to Monda that continue
    the text-processing mind set we all came from. All of them were peppered with
    Write-Host. The help and other documents must assist in the paradigm shift to
    object pipelines. They need to get objects, manipulate objects and output
    objects. In that way we exemplify the end, we have examples that can live in
    an object pipeline.
     
    Mark Ayers, May 15, 2006
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.