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


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:

2. Now copy that CSV locally and open it up.

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

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.

Find: $
Replace: ‘,
# Replace all
Find: ^
Replace: ‘
# Replace all

NOTE: 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:

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

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

For example:

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.

Many tasks will follow this same workflow so remember it!