Wednesday, December 9, 2015

PowerShell - Delete Desktop.ini Files

Ever pull up file explorer to browse for a user's home directory only to find a bunch of folders listed as "Documents"?
    

Good luck trying to figure out which "Documents" folder is the one you want.  You can still get to the user's folder if you know the full name, though.  For instance, say the home folder is called UsernameOfPerson and the file path is \\server\path. Typing in \\server\path\UsernameOfPerson will open the folder.  But if you try and browse to the folder by clicking through them, you are met with a bunch of generic folders.  This occurs due to the creation of desktop.ini files.  Deleting the desktop.ini files will revert the folders back to their original folder names.  

But how do you quickly delete a bunch of files at once? PowerShell works great. Here is a PowerShell script that will delete all desktop.ini files located in the E: drive: 

GCI -File -Path E:\ -Include desktop.ini -Recurse -Force | foreach {$_.Delete()}

The path can be changed to any path you prefer, even UNC paths.  The "-File" option tells PowerShell to only look at files, not folders.  The "-Include desktop.ini" tells PowerShell to only look for files specifically named desktop.ini.  Setting these two requirements limits the damage that can be done when deleting files with a script.  This script could easily be used in a scheduled task if needed.  As always, test the script first before implementing in production.

            Have another way to remove or limit/prevent the creation of desktop.ini files?  Let us know in the comments.

-IT Bits


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.