Get Files By Owner In PowerShell

This sample creates a PowerShell function that lists files that are owned by the specified user and optionally match a mask. By default the function will match all users and files in the current directory.

                                        
 Function Get-FilesByOwner([string] $userName = "*", [string] $fileMask = "*" ) {

  #
  # Get matching files owned by the specified user
  #
  Get-ChildItem *$fileMask* | ?{(Get-Acl $_ ).Owner -LIKE "*$userName*"} | Select FullName
 }

                                        

Examples of using this sample:

                                        
 PS C:\users\Phil> Get-FilesByOwner "Phils"

 FullName
 --------
 C:\users\Phil\.android
 C:\users\Phil\Contacts
 C:\user\Phil\Desktop
 C:\users\Phil\Documents
 C:\users\Phil\Videos
 C:\users\Phil\Dropbox              


 PS C:\users\Phil> Get-FilesByOwner "Phils" "Videos"

 FullName
 --------
 C:\users\Phil\Videos


                                        

This function uses the "?" alias which corresponds to Where-Object.


© 2024 Embrs.net