none
Simple Possibly Stupid Powershell Question RRS feed

  • Question

  • Hi Folks;

         I am having a problem doing (programatically in a script) something that works at the console / command line.

    I map a  (remote computer) new-psdrive, say "S".  Then do something like this

    $loc = "s:\users"

    $users= get-childitem $loc

    At the command line and in the script, this works, however, if I do something like

    $users=get-childitem $loc | ft -property Name, LastWriteTime

    This works at the command line and gives me trash / possibly object data types in the script.  How do I get the data programmaticly in a nicely formatted manner.

    Thanks

    swgeek

    Thursday, June 11, 2015 4:31 PM

Answers

  • I see no attempt in your script or question to use a pipeline as you think:

    This:

    $users=get-childitem $loc | ft -property Name, LastWriteTime

    Write format records into $users.  That won't work.

    This:

    get-childitem \\server\users | ft -property Name, LastWriteTime

    writes formatted text to the console.  To get that in a file we do this:

    get-childitem \\server\users | ft -property Name, LastWriteTime | Out-String  myfile.txt

    All of this is chapters 1 and 2 of almost every PowerShell book.  Learning PowerShell will remove your frustration.  You should eb ableto read two chapters in an hour.  That is about as much time as you have spent fixing one problem and you have learned one small thing incorrectly.

    Don't waste you time.  Read the manual.


    \_(ツ)_/

    Thursday, June 11, 2015 5:28 PM

All replies

  • Just use it.  It will give you a nicely formatted output.

    get-childitem \\server\users | ft -property Name, LastWriteTime


    \_(ツ)_/

    • Proposed as answer by jrv Thursday, June 11, 2015 5:28 PM
    Thursday, June 11, 2015 4:53 PM
  • I would get away from storing formatted results in a variable. I can't think of a time I've seen that done. Store your results in a variable, and if you want them formatted, pipe your variable to a formatting cmdlet. That means, $users will always be assigned Get-ChildItem $loc ($users = Get-ChildItem $loc), and if you want it formatted in a table, then pipe it: $users | Format-Table -Property Name,LastWriteTime.

    Edit: Added additional info.


    Thursday, June 11, 2015 4:57 PM
  • Never mind. I got it: you have to use write-output to display pipeline data.  That stumped me for a couple of hours of searching.  Powershell is sometimes a love / hate relationship.  I am leaving the question and answer up in case anyone else has this problem

    swgeek

    Thursday, June 11, 2015 5:23 PM
  • I see no attempt in your script or question to use a pipeline as you think:

    This:

    $users=get-childitem $loc | ft -property Name, LastWriteTime

    Write format records into $users.  That won't work.

    This:

    get-childitem \\server\users | ft -property Name, LastWriteTime

    writes formatted text to the console.  To get that in a file we do this:

    get-childitem \\server\users | ft -property Name, LastWriteTime | Out-String  myfile.txt

    All of this is chapters 1 and 2 of almost every PowerShell book.  Learning PowerShell will remove your frustration.  You should eb ableto read two chapters in an hour.  That is about as much time as you have spent fixing one problem and you have learned one small thing incorrectly.

    Don't waste you time.  Read the manual.


    \_(ツ)_/

    Thursday, June 11, 2015 5:28 PM