0

Im working on an assignment and I noticed the output breaks in my while loop when using the -AutoSize parameter on the Format-Table cmdlet.

For example: (This works)

while ( ($in = Read-Host -Prompt "Enter Option: ") -ne "5") {

    if ($in -eq "1") {
            Get-ChildItem -Path "C:\Users\User\Git\Test\Requirements1\*" -Include *.txt;
    }

    if ($in -eq "2") {
            Get-ChildItem -Path 'C:\Users\User\Git\Test\Requirements1' | Sort-Object -Property Name | Format-Table;
    }

}

I can cycle back and forth between 1 and 2. No problem.

Enter Option: : 1


    Directory: C:\Users\USER\Git\Test\Requirements1


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        7/15/2019   3:36 PM           7935 Assign1.txt
-a----        7/15/2019   9:15 AM             38 DailyLog.txt


Enter Option: : 2




    Directory: C:\Users\User\Git\Test\Requirements1


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        7/15/2019   3:36 PM           7935 Assign1.txt
-a----        7/15/2019   9:15 AM             38 DailyLog.txt
-a----         7/4/2019  10:50 AM         196608 x86ship-U.etl
-a----        7/15/2019   8:44 AM       10289152 x86ship-U.etl
-a----        7/12/2019  11:35 AM       52430553 log.bak.log
-a----        7/12/2019   4:38 PM       28564622 log.bak.log
-a----        4/27/2019  11:07 AM           4138 0.log
-a----        4/28/2019   3:00 AM           1501 00.log
-a----        4/28/2019   3:00 AM        3148824 0000.dat
-a----         7/6/2018   4:25 PM             24 Storage_OCAddin_0.dat
-a----        7/15/2019   8:45 AM          65536 x86ship-U.etl

Now if I add the -AutoSize param on Format-Table:

while ( ($in = Read-Host -Prompt "Enter Option: ") -ne "5") {

    if ($in -eq "1") {
            Get-ChildItem -Path "C:\Users\User\Git\Test\Requirements1\*" -Include *.txt;
    }

    if ($in -eq "2") {
            Get-ChildItem -Path 'C:\Users\User\Git\Test\Requirements1' | Sort-Object -Property Name | Format-Table -AutoSize;
    }

}

"1" only works the first time. After that it seems to get stuck and not show the output. But when I select 2, it shows the output from "1" and "2".

Enter Option: : 1


    Directory: C:\Users\User\Git\Test\Requirements1


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        7/15/2019   3:36 PM           7935 Assign1.txt
-a----        7/15/2019   9:15 AM             38 DailyLog.txt
Enter Option: : 2




    Directory: C:\Users\User\Git\Test\Requirements1


Mode          LastWriteTime   Length Name
----          -------------   ------ ----
-a----        7/15/2019   3:36 PM           7935 Assign1.txt
-a----        7/15/2019   9:15 AM             38 DailyLog.txt
-a----         7/4/2019  10:50 AM         196608 x86ship-U.etl
-a----        7/15/2019   8:44 AM       10289152 x86ship-U.etl
-a----        7/12/2019  11:35 AM       52430553 log.bak.log
-a----        7/12/2019   4:38 PM       28564622 log.bak.log
-a----        4/27/2019  11:07 AM           4138 0.log
-a----        4/28/2019   3:00 AM           1501 00.log
-a----        4/28/2019   3:00 AM        3148824 0000.dat
-a----         7/6/2018   4:25 PM             24 Storage_OCAddin_0.dat
-a----        7/15/2019   8:45 AM          65536 x86ship-U.etl


Enter Option: : 1

Enter Option: : 1


Enter Option: : 2

    Directory: C:\Users\User\Git\Test\Requirements1


Mode          LastWriteTime Length Name
----          ------------- ------ ----
-a----  7/15/2019   3:36 PM   7935 Assign1.txt
-a----  7/15/2019   9:15 AM     38 DailyLog.txt
-a----  7/15/2019   3:36 PM   7935 Assign1.txt
-a----  7/15/2019   9:15 AM     38 DailyLog.txt




    Directory: C:\Users\User\Git\Test\Requirements1


Mode          LastWriteTime   Length Name
----          -------------   ------ ----
-a----        7/15/2019   3:36 PM           7935 Assign1.txt
-a----        7/15/2019   9:15 AM             38 DailyLog.txt
-a----         7/4/2019  10:50 AM         196608 x86ship-U.etl
-a----        7/15/2019   8:44 AM       10289152 x86ship-U.etl
-a----        7/12/2019  11:35 AM       52430553 log.bak.log
-a----        7/12/2019   4:38 PM       28564622 log.bak.log
-a----        4/27/2019  11:07 AM           4138 0.log
-a----        4/28/2019   3:00 AM           1501 00.log
-a----        4/28/2019   3:00 AM        3148824 0000.dat
-a----         7/6/2018   4:25 PM             24 Storage_OCAddin_0.dat
-a----        7/15/2019   8:45 AM          65536 x86ship-U.etl

Can anyone explain this behavior?

jes516
  • 542
  • 1
  • 13
  • 30

1 Answers1

0

This is part of a common gotcha where a powershell script can't output more than one different set of properties as a table. PowerShell: write-output only writes one object If both those if statements ran a format-table, it would work. Or you could pipe the whole thing to format-table, but the while statement would have to be inside a function.

js2010
  • 23,033
  • 6
  • 64
  • 66