Common Workflow: Get Email Addresses For All Users On A Share

1 minute read

Description:

For example, we want to get the emails for all users who access shares on a file server.

To Resolve:

  1. First, remote into the file server and run the following:

    1
    2
    3
    4
    5
    
    Get-ChildItem e: -Recurse |
    Where-Object { $_.psiscontainer } |
    Get-Acl |
    Select-Object @{Name='Path';Expression={Convert -Path $_.Path}},Owner,AccessToString |
    out-file c:\scripts\shares.csv
    
  2. Now copy that CSV locally and open it up.

    • groups-1
  3. Use Excel’s “Text to columns” feature on the Data tab in the ribbon to convert the text to columns.

  4. Paste the “Owner” column into dedupelist.com

  5. Take those results and copy to notepad++

  6. Press Ctrl+H and choose “Regular expression” in the replace dialog. We then do the following to replace the first character with a single quote and the last characters as a single quote and a comma. This is so essentially building an array in powershell.

    1
    2
    3
    4
    5
    6
    7
    
    Find: $  
    Replace: ',  
    # Replace all  
       
    Find: ^  
    Replace: '  
    # Replace all
    
    • This part takes discretion and sometimes you might have other data you need to remove. For example, I had to switch to regular find/replace and remove domainName\ from each line, just replace with (blank) <- Don’t type anything and then click “Replace all” in normal find/replace mode.

    • Now on line one, just type: $users = @( and press enter:

    • groups-2

    • Now go to the last line and delete the comma and place a closing parenthesis to close the array.

    • groups-3

  7. Now that you have an array, you can do just about anything you need to do. For example:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    Import-Module activedirectory
    $UserArray = [System.Collections.Generic.List[PSObject]]@()
    $EmailArray = [System.Collections.Generic.List[PSObject]]@()
    ForEach ($u in $users)
    {
    $User = Get-ADUser -Identity $u
    [void]$UserArray.Add($($User.Name))
    [void]$EmailArray.Add($($user.UserPrincipalName))
    }
    
    • This will import AD module and initialize two new arrays. It will then loop through each member in the first array and pull their username and email and put them in different arrays. You can then take $EmailArray and export it to CSV or something. Or just copy and paste the results from the screen into an email.
  8. Many tasks will follow this same workflow so remember it!

Comments