Solved

Powershell Script to Export Filenames and Directories by Owner

Posted on 2014-03-04
7
834 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 500 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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 this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

624 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