简体   繁体   中英

Powershell Get-ChildItem - Missing header on subsequent calls to same path

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!

  $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

get-childitem之后添加format-table ,它将始终以带有标题的表格式显示结果

Get-childitem $path | format-table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM