Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 886
  • Last Modified:

Powershell Script to Export Filenames and Directories by Owner

I need a script I can run in a directory where I can list all files, their names, directories by a specific owner.  I'm presently using this script I found:

$arr = @()
gci "F:\Shares" -recurse | ? {$_.PSIsContainer -eq $False} | % {
  $obj = New-Object PSObject
  $obj | Add-Member NoteProperty Directory $_.DirectoryName
  $obj | Add-Member NoteProperty Name $_.Name
  $obj | Add-Member NoteProperty Length $_.Length
  $obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
  $arr += $obj
  }
  $arr | Export-CSV -notypeinformation "c:\fileowner.csv"

Open in new window


This is good, but I would like to have the search performed by Owner instead, and output the directory and name of those that are associated with the specified owner.
0
fireguy1125
Asked:
fireguy1125
  • 4
  • 3
1 Solution
 
Jason RybergCommented:
I think I have an idea of what you're asking.  Add the following after the ForEach loop to group the array by Owner:

$arr | Group-Object owner

Open in new window


If you are still wanting to export to CSV, you'll need to loop through the Group object hashtable and convert it into a string type object.
0
 
fireguy1125Author Commented:
Sorry - not really familiar with the scripting logic - would you be able to provide the script inclusive of what you supplied?

Thanks.
0
 
Jason RybergCommented:
You can just add my line before the last line of your script to group by owner.  This will help you perform functions within PowerShell on groups of files and directories based on their owners.

$arr = @()
gci "F:\Shares" -recurse | ? {$_.PSIsContainer -eq $False} | % {
  $obj = New-Object PSObject
  $obj | Add-Member NoteProperty Directory $_.DirectoryName
  $obj | Add-Member NoteProperty Name $_.Name
  $obj | Add-Member NoteProperty Length $_.Length
  $obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
  $arr += $obj
  }
$ByOwner = $arr | Group-Object owner

$ByOwner[0].group #will show the files of just the first owner

Open in new window


I'm curious now, however, what your end goal is, because I feel like I'm not getting entirely what you're after.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
fireguy1125Author Commented:
Sorry - so the script essentially lists all files, folders, sizes, and owners.  What I basically want is just the results of the csv file to populate only with a specified owner name.  

Presently what I'm doing now with all the files and folders listed, is manually having to sort by the Owner column, and delete the range that don't correspond to the owner I am looking for.

I don't want a list of all folders/files with their owners, instead, using a specified owner name, list the folders/files that belong to that specified owner.

Hope that clears things up.
0
 
Jason RybergCommented:
If you're looking to narrow the results to a single owner before exporting, insert this line before the last line of your original script (it filters the $arr object by the Jsmith owner.  Obviously, you'll need to change Jsmith to whatever owner you're looking for):

$arr | ? { $_.owner -eq "Jsmith" }

Open in new window

0
 
fireguy1125Author Commented:
I added the line as second to last, however when I run it, it still returns all owner results.
0
 
Jason RybergCommented:
$arr = @()
gci "F:\Shares" -recurse | ? {$_.PSIsContainer -eq $False} | % {
  $obj = New-Object PSObject
  $obj | Add-Member NoteProperty Directory $_.DirectoryName
  $obj | Add-Member NoteProperty Name $_.Name
  $obj | Add-Member NoteProperty Length $_.Length
  $obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
  $arr += $obj
}
$oneOwner = $arr | ? { $_.owner -eq "Jsmith" }
$oneOwner | Export-CSV -notypeinformation "c:\fileowner.csv"

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now