Windows Vista Forums

Capture command result

  1. #1


    Bruce Wilkinson Guest

    Capture command result

    How do a capture the result of a command in a script? For example, the
    following will return the file last written, dir | sort -prop LastWriteTime |
    select -last 1. How do I save that so I can later use the name?

    Bruce

      My System SpecsSystem Spec

  2. #2


    RichS Guest

    RE: Capture command result

    Could try something like this

    PS> $t = dir | sort -prop LastWriteTime | select -last 1
    PS> $t.name
    test1
    PS>


    --
    Richard Siddaway

    Please note that all scripts are supplied "as is" and with no warranty


    "Bruce Wilkinson" wrote:

    > How do a capture the result of a command in a script? For example, the
    > following will return the file last written, dir | sort -prop LastWriteTime |
    > select -last 1. How do I save that so I can later use the name?
    >
    > Bruce


      My System SpecsSystem Spec

  3. #3


    Andrew Watt [MVP] Guest

    Re: Capture command result

    On Tue, 19 Dec 2006 08:30:00 -0800, Bruce Wilkinson
    <BruceWilkinson@discussions.microsoft.com> wrote:

    >How do a capture the result of a command in a script? For example, the
    >following will return the file last written, dir | sort -prop LastWriteTime |
    >select -last 1. How do I save that so I can later use the name?
    >
    >Bruce


    Bruce,

    I assume you want to save the information in a file. If not then
    simply assign it to a variable as in the first line of code which
    follows.

    The following works for me.

    $a = dir | sort -prop LastWriteTime | select-object -Last 1
    $a > C:\Last.txt

    NOTE: The following does NOT work. At least it doesn't work as
    expected for me.

    (dir | sort -prop LastWriteTime | select-object -Last 1) > C:\Last.txt

    Andrew Watt MVP

      My System SpecsSystem Spec


Capture command result
Similar Threads
Thread Forum
Referencing .NET 2.0 but end result uses .NET 3.5 .NET General
changing the format of date command result PowerShell
Parsing email; running a command; and returning the result. PowerShell
capture command and its output PowerShell
How can you capture individual thumbnails not full capture ? Vista music pictures video