Adding to powershell array

I have 2 files, each with data (actually each with only 1 column of data) I want merged into 1 file with 2 columns:

The 2 files are 11.txt and 12.txt

I want 13.txt to have column A with the data from 11.txt and column B to have the data from 12.txt

Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName | Export-Csv -path C:\11.txt -notype

$a = import-csv "c:\11.txt"

$a | foreach ($_) {(Get-ChildItem $_.fullname).Count} > C:\12.txt

My searched for this have been leading me to merging the files but with the assumption that the files each have the same header information.

Any help is appreciated.

Thanks

January 20th, 2015 1:10am

Instead of trying to stick two files together, you can do everything at once:

Get-ChildItem -Directory -Recurse | ForEach {

    $props = @{
        Folder = $_.FullName
        Count = (Get-ChildItem $_.FullName).Count
    }

    New-Object PsObject -Property $props

} | Sort Folder | Export-Csv .\folderCount.csv -NoTypeInformation
Free Windows Admin Tool Kit Click here and download it now
January 20th, 2015 1:53am

New method for V3 and later:

Get-ChildItem -Directory -Recurse | 
    ForEach {
        [pscustomobject]@{
            Folder=$_.FullName
            Count=$_.GetFiles().Count
        }
    } |
    Sort Folder | 
    Export-Csv .\folderCount.csv -NoTypeInformation

Also faster an makes fewer round trips.

January 20th, 2015 3:20am

I tried to paste this into powershell and I get this:

Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.
At line:1 char:25
+ Get-ChildItem -Directory <<<<  -Recurse | ForEach {
    + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

I am wondering if, this is happening because a folder is empty.

Also When I paste this into powerhshell, I need to press enter twice. I'm not sure if this is supposed to be saved as a PS1, and then run.

Free Windows Admin Tool Kit Click here and download it now
February 17th, 2015 3:40pm

I tried to paste this into powershell and I get this:

Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.
At line:1 char:25
+ Get-ChildItem -Directory <<<<  -Recurse |
    + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

I am wondering if, this is happening because a folder is empty.

Also When I paste this into powerhshell, I need to press enter twice. I'm not sure if this is supposed to be saved as a PS1, and then run.

February 17th, 2015 3:42pm

Hi,

The -Directory switch requires at least v3 of PowerShell. If you're stuck with v2, try this adjustment:

Get-ChildItem -Recurse | Where { $_.PsIsContainer} | ForEach {

    $props = @{
        Folder = $_.FullName
        Count = (Get-ChildItem $_.FullName).Count
    }

    New-Object PsObject -Property $props

} | Sort Folder | Export-Csv .\folderCount.csv -NoTypeInformation

Free Windows Admin Tool Kit Click here and download it now
February 17th, 2015 3:44pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics