none
Format output of get-content RRS feed

  • Question

  • Hello everyone, i have a list of computers from which i need to know the last logon date, so i am runnnig the following:

    get-content C:\temp\Computers.txt | % {Get-ADComputer -identity $_ -Properties * | FT Name, LastLogonDate} | format-list name | Out-File C:\Temp\ComputerLastLogonDate.txt

    It gives me the following output:

    Name                                    LastLogonDate                          
    ----                                    -------------                          
    XXXXX                                2/23/2016 2:30:39 AM                   



    Name                                    LastLogonDate                          
    ----                                    -------------                          
    YYYYY 2/22/2016 7:35:45 AM                   

    Which is something i cant work with, the question is, does anybody knows how to format the output to something like:

    Name                                    LastLogonDate                          
    ----                                    -------------                          
    YYYYY 2/22/2016 7:35:45 AM         

    XXXX 2/23/2016 2:30:39 AM 

    Thanks in advance!

    Tuesday, March 1, 2016 10:16 PM

Answers

  • -Properties * is inefficient.

    Read this article for help.

    PowerShell: Objects and Output


    Get-Content C:\temp\Computers.txt | ForEach-Object {
      Get-ADComputer -Identity $_ -Properties LastLogonDate
    } |
      Select-Object Name,LastLogonDate |
      Export-Csv C:\temp\Report.csv -NoTypeInformation
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Matt Malpica Tuesday, March 1, 2016 11:08 PM
    Tuesday, March 1, 2016 10:21 PM
    Moderator

All replies

  • -Properties * is inefficient.

    Read this article for help.

    PowerShell: Objects and Output


    Get-Content C:\temp\Computers.txt | ForEach-Object {
      Get-ADComputer -Identity $_ -Properties LastLogonDate
    } |
      Select-Object Name,LastLogonDate |
      Export-Csv C:\temp\Report.csv -NoTypeInformation
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Matt Malpica Tuesday, March 1, 2016 11:08 PM
    Tuesday, March 1, 2016 10:21 PM
    Moderator
  •  I don't see the difference other than  the spacing.

    \_(ツ)_/

    Tuesday, March 1, 2016 10:35 PM
  • This is maybe because there is confusion about how PowerShell outputs objects using the default formatter.

    CSV is good, lightweight, and versatile.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, March 1, 2016 10:42 PM
    Moderator
  • The difference is that for each line is writing name and lastlogon date against having it as a list which would make it easier to work with....
    Tuesday, March 1, 2016 10:45 PM
  • When i use CSV it gives me the same format

    Tuesday, March 1, 2016 10:47 PM
  • What do you mean? You will have to post an example. (Remember, we can't see your screen.)

    -- Bill Stewart [Bill_Stewart]

    Tuesday, March 1, 2016 10:49 PM
    Moderator
  • 

    This is the ouput, i need to be able to sort the results etc...

    Tuesday, March 1, 2016 10:52 PM
  • This is the result as TXT

    Tuesday, March 1, 2016 10:54 PM
  • You are forcing the output as text.

    Output as CSV instead, like in my example. It will read directly into your spreadsheet when you open it.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, March 1, 2016 10:56 PM
    Moderator
  • no, that output is using the command:

    get-content C:\temp\Computers.txt | % {Get-ADComputer -identity $_ -Properties * | FT Name, LastLogonDate -Autosize} | out-file c:\temp\matt.csv

    Tuesday, March 1, 2016 11:01 PM
  • Do not use Out-File.

    Read my example very carefully. Do not try formatting the output on your own. You don't need to.

    Just use Export-Csv.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, March 1, 2016 11:04 PM
    Moderator
  • sorry i just saw your command...
    Tuesday, March 1, 2016 11:06 PM