Does anyone know how you can display output from a function like get-childitem when it runs multiple times(array pipeline input)? I’ll try to show it With an example.
PS C:\Users> ".\Graimer", ".\Public" | Get-ChildItem
Directory: C:\Users\Graimer
Mode LastWriteTime Length Name
---- ------------- ------ ----
d-r-- 20.12.2012 15:59 Contacts
d-r-- 06.01.2013 01:23 Desktop
d-r-- 02.01.2013 17:15 Documents
d-r-- 05.01.2013 16:38 Downloads
d-r-- 20.12.2012 15:59 Favorites
d-r-- 20.12.2012 15:59 Links
d-r-- 20.12.2012 15:59 Music
d-r-- 20.12.2012 15:59 Pictures
d-r-- 20.12.2012 15:59 Saved Games
d-r-- 20.12.2012 15:59 Searches
d-r-- 20.12.2012 15:59 Videos
Directory: C:\Users\Public
Mode LastWriteTime Length Name
---- ------------- ------ ----
d-r-- 20.12.2012 13:57 Documents
d-r-- 26.07.2012 10:13 Downloads
d-r-- 26.07.2012 10:13 Music
d-r-- 26.07.2012 10:13 Pictures
d-r-- 26.07.2012 10:13 Videos
In my function, when I use array input and process { } Blocks, Write-output puts all the results in one table like it should. But how can I format it like this? Separate by Directory when displayed, while being stored as a standard object-array? Can’t seem to find articles on it. It doesn’t matter if it’s only possible With compiled cmdlets, I’m just looking for some clues no matter what 🙂
EDIT: Added my code. Just creating psobjects and using Write-Object to display them.
Function Get-RecurseFileCount
{
[CmdletBinding()]
Param
(
[Parameter(ValueFromPipeline=$true)]
[String[]]$Path = "."
)
Begin
{
}
Process
{
foreach ($rootpath in $Path)
{
Write-Verbose "Testing if path of rootdirectory exists"
if(Test-Path $rootpath)
{
Write-Verbose "Getting all recursive subfolders"
$folders = Get-ChildItem $rootpath -Recurse -ErrorAction SilentlyContinue | where {$_.PSIsContainer}
Write-Verbose "Starting to count files in rootfolder"
$folder = Get-Item $rootpath
$fcount = (Get-ChildItem $rootpath -ErrorAction SilentlyContinue | where {!$_.PSIsContainer}).Count
New-Object psobject -Property @{FolderName = $folder.Name; FolderPath = $folder.FullName; FileCount = $fcount} | Write-Output
Write-Verbose "Starting to count files in subfolders"
foreach($folder in $folders)
{
$fname = $folder.Name
$fpath = $folder.FullName
$fcount = (Get-ChildItem $fpath -ErrorAction SilentlyContinue | where {!$_.PSIsContainer}).Count
New-Object psobject -Property @{FolderName = $fname; FolderPath = $fpath; FileCount = $fcount} | Write-Output
}
Write-Verbose "Finished with filecount"
}
}
}
End
{
}
}
And Format-Table -GroupBy .. is not an answer. A solution would be to add a property called root = $rootpath and add a default view that Groups by the property root AND hiding it in the table so it’s not displayed multiple times. The question is just.. how? 🙂
You have to provide the
GroupByinformation in your view in aformat.ps1xmlfile and update the format data to get the desired behaviour. There is no getting out of writing theps1xmland loading it in your session.http://technet.microsoft.com/en-us/library/dd315396.aspx