Outputting variable value

Hi everyone,

I am new to PowerShell so apologies if I don’t explain this clearly. I have a file that contains a list of directories/folders in a CSV. I want to loop over each of those file paths and return the users who have access to that file directory/folders. So far I have the following script:

$csv = import-csv “random_pathway\file.csv”

$csv | foreach-object {
** $folder = $.Folder_Path**
**get-acl $folder | ForEach-Object { $
.Access } | select IdentityReference**
}

It is returning a list of users which is great however I need to script to return the variable $folder for each row so I know which folder in this list the user has access to.

Current Output

IdentityReference
User1
User2
User3
User4
User5

Desired Output

IdentityReference Folder
User1 Folder1
User2 Folder1
User3 Folder2
User4 Folder2
User5 Folder3

I need the 2nd column to output the folder that the script that looped over as well.

Any help would be greatly appreciated :slight_smile:

Shane,
Welcome to the forum. :wave:t4:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

You can use a [PSCustomObject] to combine the results of two or more different queries. Something like this hsould do the trick:

$csv = Import-Csv -Path 'random_pathway\file.csv'

$csv | 
ForEach-Object {
    $ACLList = Get-Acl $_.Folder_Path
    foreach ($ACL in $ACLList) {
        [PSCustomObject]@{
            Folder = $_.Folder_Path
            User   = $ACL.Access.IdentityReference
        }
    }
}
1 Like

HI Olaf,

Thank you so much this is really helpful.

Much appreciated :slight_smile:

Shane

Could I ask one more question. The script works fine and returns the information I need however when I export the data to a csv it is truncating the string in each row and is returning … I had a quick google, apparently -AutoSize is supposed to return all the data in the output but the rows are still be truncated, any solutions?

$csv = Import-Csv -Path "random_pathway\file.csv"
$csv | 
ForEach-Object {
    $ACLList = Get-Acl $_.Folder_Path
    foreach ($ACL in $ACLList) {
        [PSCustomObject]@{
            Folder = $_.Folder_Path
            Owner = $_.Owner
            User   = $ACL.Access.IdentityReference
        } 
    } 
} |Format-Table Folder, Owner, User -AutoSize  | Out-File "random_pathway\output_file.csv"

Thanks in advance.

You’re not exporting CSV, you are creating custom string output. Remove the format table and out file commands and instead use select-object to grab the desired properties and pipe that to export-csv

1 Like