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
I'm using DevStudio 2005 and I have a project that references System.dll ver 2.0.50727 and I accidentally compiled code that used an overload that...
.NET General
changing the format of date command result
I need to fix a problem with batch files that it has the command "echo %date%" inside them . So , I run them in a lot of machines and every machine...
PowerShell
Parsing email; running a command; and returning the result.
Hi guys. I've been out of the Powershell scene for a while because I got a new job outside of IT, but I've wanted to do something that would help...
PowerShell
strange result when adding $
Hello everyone, 1. $a=0 $x=($a++) then $x is 0. but
PowerShell
capture command and its output
Hi all I am in the process of learning PS so I am trying the different cmdlets to see wht they do what the output is etc. I would like to log...
PowerShell
How can you capture individual thumbnails not full capture ?
I am capturing my video from my Sony HDR-HC5 in HD into windows movie maker fine, but it captures as 1 thumbnail in dvr-ms format ie full lenght of...
Vista music pictures video
Search Result
How to view search result in normal (without underline)..................when i type any filename in start icon---->start search......... the...
Vista General