Solved

Powershell Script to Export Filenames and Directories by Owner

Posted on 2014-03-04
7
813 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

734 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