Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell Script to Export Filenames and Directories by Owner

Posted on 2014-03-04
7
Medium Priority
?
869 Views
Last Modified: 2014-04-03
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
Comment
Question by:fireguy1125
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 4

Expert Comment

by:Jason Ryberg
ID: 39904935
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
 
LVL 1

Author Comment

by:fireguy1125
ID: 39924151
Sorry - not really familiar with the scripting logic - would you be able to provide the script inclusive of what you supplied?

Thanks.
0
 
LVL 4

Expert Comment

by:Jason Ryberg
ID: 39926307
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
LVL 1

Author Comment

by:fireguy1125
ID: 39935585
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
 
LVL 4

Expert Comment

by:Jason Ryberg
ID: 39937207
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
 
LVL 1

Author Comment

by:fireguy1125
ID: 39937590
I added the line as second to last, however when I run it, it still returns all owner results.
0
 
LVL 4

Accepted Solution

by:
Jason Ryberg earned 2000 total points
ID: 39937952
$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

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question