Make This Tech Work (Archive)

Make a list of all your DRM protected music and video files in a folder using PowerShell (Windows)

2019-05-22 17:09:59

Would you like to quickly locate just the DRM protected files in a folder tree on a hard drive in Windows?  DRM protected files are files that have copyright protection built-in so that they only play if you have correct access, for example, access to purchased music through your iTunes account.    

"Protected" property for a file in Windows Explorer

Music and video files in Windows have a file attribute that indicates if the file is "protected" or not. You can see this property if you right-click a music or video file (for example an mp3 file) in Windows Explorer and click "Properties". Then click the "Details" tab and scroll down to the "Content" section:

Find DRM Protected Files including music and video files

This data is stored in something called "metadata" in the file's properties on disk.

Obviously, using Windows Explorer, it would take quite a while to see properties for many files in a folder recursively. And if you wanted to do something with just these files, you would need a list of paths to just those files. Enter PowerShell. To quickly make a list of which music/video files in an entire folder tree are protected, you can do the following:

Using a PowerShell function in a script to get all Metadata for files in a folder recursively

First, you can use the following PowerShell script/function which will retrieve all the Metadata for any music and video files (source).  

You simply want to copy just the following (from that web site) to the clipboard by highlighting the text and pressing ctrl-c.

Function Get-FileMetaData 
{  
Param([string[]]$folder) 
foreach($sFolder in $folder) 
  { 
   $a = 0 
   $objShell = New-Object -ComObject Shell.Application 
   $objFolder = $objShell.namespace($sFolder) 
 
   foreach ($File in $objFolder.items()) 
    {  
     $FileMetaData = New-Object PSOBJECT 
      for ($a ; $a  -le 266; $a++) 
       {  
         if($objFolder.getDetailsOf($File, $a)) 
           { 
             $hash += @{$($objFolder.getDetailsOf($objFolder.items, $a))  = 
                   $($objFolder.getDetailsOf($File, $a)) } 
            $FileMetaData | Add-Member $hash 
            $hash.clear()  
           } #end if 
       } #end for  
     $a=0 
     $FileMetaData 
    } #end foreach $file 
  } #end foreach $sfolder 
} #end Get-FileMetaData 

After copying that script to the clipboard, do the following:

1) open Windows PowerShell by pressing Windows-S to open search, and type "powershell", then click "Windows PowerShell"

2) In the PowerShell window, paste the clip copied from the clipboard into the Windows PowerShell window (press ctrl-v), and press enter to load the script into memory.

3) Now, type in the following into that PowerShell window to execute the function and store the results into a variable called “fileMetadata”:

Note that this may take a while to complete if you have a large number of files to process.  

Export three file properties, Path, Protected, and Type to a CSV file

Once the Get-FileMetaData PowerShell command above has completed, run the following PowerShell command (same window as before) to export three file properties (Path, Protected, and Type) to a CSV file:

Edit the CSV file in Google sheets to narrow down the output to just the files with the "Protected" property set to "yes"

Now open the CSV file in Google Sheets and sort the rows, eliminating any rows you don't need, for example, rows that contained folders instead of files.  Once you remove all unneeded rows, you should see only rows that contain files with the protected attribute set to yes. You should have something similar to this:

Now you can operate on this list of files.  

Delete only files with the "Protected" property set to "yes"

If, for example, you wanted to delete these files you can do the following:

1) Copy the file path (a single column in the CSV file) for those files to a new file in noteapad++ (download notepad++ if you don't already have it).  

Copy column of file paths

2) Use the macro feature in notepad++ to quickly add "DEL " in front of every file and quotes around the file path.  

3) Then simply save this file as a windows batch file (for example deleteprotectedfiles.bat) and run this batch file at the Window's Command prompt.

This deletes all DRM protected files from my saved files in a particular folder.  

Delete empty directories that are leftover (Robocopy method; preferred)

After deleting the DRM protected files, you might be left with empty directories. One final step after the delete if you want to remove empty directories.  To delete empty directories in a folder structure, simply run this command:

ROBOCOPY folder1 folder1 /S /MOVE
(source)

where "folder1" is the folder that contains all your music and video files (and has empty directories to delete).

Delete empty directories that are leftover (PowerShell method)

If you prefer not to use the ROBOCOPY method above for deleting empty directories recursively in a folder, you can run the following PowerShell command.  Replace “c:\pathToFiles” with your folder path containing your empty directories.  

Get-ChildItem -Path "c:\pathToFiles" -Recurse -Directory | ForEach-Object -Process { if ($false -eq $_.GetFileSystemInfos()) { $_.FullName } } > tmp.bat

This adds all the empty directory paths in “c:\pathToFiles” to a file called tmp.bat.  Then use the macro editor in notepad++ to add "rd " in front of all the empty directory paths listed in that file, tmp.bat.  And finally, run this batch file at the Windows command prompt. The downside to this second method is that you may need to rerun the PowerShell command, edit the tmp.bat, and run the batch file several times in order to finally delete all empty directories.  This is because removing empty directories each time may leave a parent directory now empty. This is why the Robocopy method for deleting empty directories in a directory tree is preferred.

Sony Turntable Review

Sony NW-A45 Walkman with Hi-Res Audio review