Home > Software engineering >  PowerShell - find a "mysterious" file
PowerShell - find a "mysterious" file

Time:12-23

I have a folder with 67045 files (it should have only 67044). When I run this script:

Get-ChildItem -Recurse -Include  *.pdf | Group-Object Extension -NoElement

I get all 67044 pdf files, exactly how it should be, but I can't get whats that other file. How can I find it and delete it manually after I confirm that I don't really need that file, or in the worst case, delete everything if it's not PDF.

CodePudding user response:

Get-ChildItem -Recurse -Path C:\you\path -Include  *.pdf | Group-Object Extension -NoElement

This is giving you the files which are only PDF and the total count of it.

Just alter it to exclude

Get-ChildItem -Recurse -Path C:\your\path -Exclude  *.pdf | Group-Object Extension -NoElement

This will give you all the other file extensions apart from pdf and their corresponding count

Now the other part of it is as per the requirement, you do not need to group to figure out what are the other files available other than pdf. Follow the below:

Get-ChildItem -Recurse -Path C:\Scripts\ -Exclude  *.pdf

This will give whatever you are looking for and you can further use Pipe to remove the item like | Remove-Item -Force

Hope it helps.

CodePudding user response:

Your Question is

I can't get whats that other file. How can I find it and delete it manually after I confirm that I don't really need that file,

It is not unusual depending on location and method to get a higher file count than expected here on the desktop I can find 4 perhaps unexpected "mysterious entries".

Normally if using thumbnails that may also add thumbs.db to the file count.

I am intentionally selecting one of the many system hidden files to make my point but note the inclusion of both the public ini and the sub folder called Desktop.

Thus a right click count of total files would add those 4 and many others.

enter image description here

We can see this more clearly in a folder with just one file the properties count says there are 7

enter image description here

Powershell will naturally say there is only one docx

to get a truer count of files (7 in this case) we can use a simple command

enter image description here

So to get a truer answer to your question as to what is the mysterious file use

 Forfiles /m * |find /i /v ".pdf"

Which will exclude the .PDFs and it should then become apparent if the "Mystery" file is a folder or hidden system file.

If it is the folder.ini you should not delete it.

  • Related