Ben_Coding
Ben_Coding

Reputation: 408

How to Pipe Multiple Commands to CSV File [Powershell]

I'm trying to get The Folder Info and Security Info for all the folders on our server. But I'm not to familiar with Powershell here. Mind helping a newbie?

How to do I get the Security acl piped into the Text file? Along with just the member objects of Folder Name, Size, sub folder count?

# Step 1 Get Folder Path 
function Select-Folder($message='Select a folder', $path = 0) { 
    $object = New-Object -comObject Shell.Application  

    $folder = $object.BrowseForFolder(0, $message, 0, $path) 
    if ($folder -ne $null) { 
        $folder.self.Path 
    } 
} 


#Step 2:Search For Directories
$dirToAudit = Get-ChildItem -Path (Select-Folder  'Select some folder!') -recurse | Where {$_.psIsContainer -eq $true}

foreach ($dir in $dirToAudit)
{

#Step 3: Output: [Folder Path, Name, Security Owner, Size, Folder Count]
#Pipe To CSV Text File
    Get-Acl -Path $dir.FullName | Select-Object PSPath, Path,Owner | export-csv C:\temp\SecurityData.csv
    #I also want the Folder path, Size and SubFolder Count
}


#Step 4: Open in Excel 
invoke-item -path C:\temp\SecurityData.csv

Here's some sites that I found useful on the subject: http://blogs.msdn.com/b/powershell/archive/2007/03/07/why-can-t-i-pipe-format-table-to-export-csv-and-get-something-useful.aspx

http://www.maxtblog.com/2010/09/to-use-psobject-add-member-or-not/

Upvotes: 1

Views: 4820

Answers (1)

Keith Hill
Keith Hill

Reputation: 201592

This task isn't particularly easy. First you will want to create a custom object that contains the properties you want. These properties will be added via different commands e.g.:

$objs = Get-ChildItem . -r | 
            Where {$_.PSIsContainer} | 
            Foreach {new-object psobject -prop @{Path=$_.FullName;Name=$_.Name;FolderCount=$_.GetDirectories().Length}}
$objs = $objs | Foreach { Add-Member NoteProperty Owner ((Get-Acl $_.Path).Owner) -Inp $_ -PassThru}

$objs | Export-Csv C:\temp\data.csv

Getting the folder size will take some extra work to compute.

Upvotes: 2

Related Questions