• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Capture command result

B

Bruce Wilkinson

#1
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 Computer

R

RichS

#2
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 Computer

A

Andrew Watt [MVP]

#3
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 Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)