Solved

Powershell Script to Export Filenames and Directories by Owner

Posted on 2014-03-04
7
703 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
  • 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now