PowerShell : Find all Files Owned By A User

The Windows 10 release is just around the corner and I’m going to do a fresh install on a new hard-drive.  I’ve redirected most of my working directories to network locations so most of my files should just appear ‘auto-magically’ but I’m sure there’s a few files dotted around the place that are important that I’ve just ‘temporarily’ left outside one of the redirected folders.

So what I want is some PowerShell to find all the files owned by me on my hard-drives.  I can do this with a one-line PowerShell command as follows;Here’s the command;

Get-PSDrive | ? {$_.Provider -match "Filesystem"} | Select -ExpandProperty Root |
gci -recurse -File | get-acl | ? {@("Server\User1") -contains $_.Owner} |
Select @{Name="FullName";Expression={($_.Path -split "::")[1]}},Owner

The command breaks down as follows;

Get-PSDrive | ? {$_.Provider -match "Filesystem"} | Select -ExpandProperty Root |

The first part of the command gets all the PowerShell drives on the machine and then filters only the drives that are marked as part of the filesystem.  PSDrives can refer to the registry, directory or custom locations so I need this filter in place.

I only want the actual name of the filesystem (“C:\” etc) rather than the entire drive so I select and expand the Root property;  this just passes the names on along the pipeline.

If you know the drive or drives you want to process you could just pass them instead (ie, replace this part of the command with “C:\” | .

gci -recurse -File | get-acl | ? {@("Server\User1") -contains $_.Owner} |

This part of the command is passed the root of a filesystem and gets all the files (recursively so it checks sub-folders) with gci (Get-ChildItem).  For each of these files it gets the acl information object and passes that to a Where-Object command (shortened to ?).  Here PowerShell checks if the object’s owner is listed in an array of user names I’ve provided.  In this case I only have one entry in the list (“Server\User1“) but if I wanted to get all the files owned by a list of users I could just add more than one entry.

Select @{Name="FullName";Expression={($_.Path -split "::")[1]}},Owner

I don’t need all the information from Get-Acl so here I create a custom object with two fields;  FullName which is a custom expression equal to everything after “::” in the path and the Owner of the file.

Get-AcL‘s returned object has an attribute called Path which has the format [Path Type]::[Actual Path].  As I only want the actual path I use -split to create an array of two strings;  one before the “::” and one afterwards.  I then use [1] to get the second entry in that array (as arrays start at [0]) and that should be the file path.

This command returns a list of all the files on the local disks which have Server\User1 as the owner.

2 thoughts on “PowerShell : Find all Files Owned By A User”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s