2

Trying to figure out how to get Powershell to display header details on subsequent requests to the same directory path.

Here is a simplified example of what I am trying to do, note that the second call to Get-ChildItem does not display the header details (presumably because it knows its already been called previously within the same scriptblock):

PS C:\TEMP\foo> $path="c:\temp\foo";Get-ChildItem -Path $path;Write-Output "Delete something and display directory contents again...";del $path\*5*;Get-ChildItem -Path $path

Directory: C:\temp\foo

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         9/21/2016   9:54 PM         16 File1.txt
-a---         9/21/2016   9:54 PM         16 File2.txt
-a---         9/21/2016   9:54 PM         16 File3.txt
-a---         9/21/2016   9:54 PM         16 File4.txt
-a---         9/21/2016   9:54 PM         16 File5.txt

Delete something and display directory contents again...

-a---         9/21/2016   9:54 PM         16 File1.txt
-a---         9/21/2016   9:54 PM         16 File2.txt
-a---         9/21/2016   9:54 PM         16 File3.txt
-a---         9/21/2016   9:54 PM         16 File4.txt

This appears to be the default behavior if the same path is referenced more than once. I have found that a second header will be generated whenever a different path is provided in the second Get-ChildItem call, but never when the same path is used more than once.

Any ideas on how to force the second header to display like the first while still keeping both of these calls within the same scriptblock?

Thanks!

2 Answers2

0
  $path="c:\temp\data";

  Get-ChildItem -Path $path
  Write-host "Delete something and display directory contents again..."
  del $path\*5* -Recurse
  Get-ChildItem -Path $path
Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • I need to execute this as a string within a scriptblock not as separate commands. If you execute it as such you will see the same behavior persists. $path="c:\temp\data";Get-ChildItem -Path $path;Write-host "Delete something and display directory contents again...";del $path\*5* -Recurse;Get-ChildItem -Path $path – electrocreative Oct 30 '16 at 15:15
0

Add format-table after get-childitem,it will always display the result in table format with headers

Get-childitem $path | format-table
Diligent Key Presser
  • 4,183
  • 4
  • 26
  • 34
Prosenjit
  • 145
  • 1
  • 2
  • 10
  • `Format-Table` by itself results in this error: `out-lineoutput : The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or in correct sequence...caused by "format-table" command conflicting with default formatting` I can get around this by piping to an `Out-String` command, however the `Out-Host` solution by @PetSerAl is more direct with same result: $path="c:\temp\foo";Get-ChildItem -Path $path;Write-Output "Delete something and display directory contents again...";del $path*5*;Get-ChildItem -Path $path | Format-Table | Out-String – electrocreative Oct 31 '16 at 16:52