Search code examples
powershellsmb

Get-SMBShare save that output to a CSV


I want to export output to a CSV. But I can't export correctly.

foreach ($share in (Get-SmbShare)) {
    $share | Get-SmbShareAccess | % {
        $_ |
            Add-Member -Name 'Path' -Value $share.Path -MemberType NoteProperty -PassThru |
            Select Name, Path, AccountName, AccessControlType, AccessRight
    }
}

Solution

  • Assign the output from the foreach(){...} loop to a variable, then pipe that to Export-Csv:

    $shareAccess = foreach ($share in (Get-SmbShare)) {
        $share | Get-SmbShareAccess | % {
            $_ |
                Add-Member -Name 'Path' -Value $share.Path -MemberType NoteProperty -PassThru |
                Select Name, Path, AccountName, AccessControlType, AccessRight
        }
    }
    
    $shareAccess |Export-Csv .\path\to\output.csv -NoTypeInformation