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:
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
  1. Now copy that CSV locally and open it up.

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

  2. Paste the “Owner” column into http://www.dedupelist.com

  3. Take those results and copy to notepad++

  4. 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.

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

For example:

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.

Many tasks will follow this same workflow so remember it!