PowerShell: Quick Way to Check for a File Type on a Drive or Directory

Contributor Icon Contributed by shamanstears Date Icon June 7, 2007  
Tag Icon Tagged: Windows

Instead of performing a search on a drive or directory to see if files of a specific file type reside on it, you can save a lot of time by using PowerShell. It will come back with a True or False response in a matter of seconds instead of having to wait for the search to complete. This comes in handy if you are trying to analyze what file types are being stored on your server (for example, you want to make sure no one is stashing their MP3 collection on it!)


1. Open PowerShell.

2. You will use the test-path cmdlet, the directory or drive path, and a wildcard expression, so if we were checking the Data directory on the C Drive for MP3 files, it would look like this:test-path C:\Data\*.mp3

3. Press Enter.

4. You will receive either a True or False response.

Previous recipe | Next recipe |
 
  • Anonymous
    Can this be done recursively? In other words, can I test-path for all files in the path and every subdirectory of the path?

    btw, thanks for a great blog.
  • Anonymous
    does it have to be powershell? without installing powershell, you can use vbscript
    Set objFSO = CreateObject("Scripting.FilesyStemObject")
    myFolder="c:"
    Set objFolder = objFSO.GetFolder(myFolder)
    GoSubFolders objFolder

    Sub GoSubFolders (objDIR)
    If objDIR <> "System Volume Information" Then
    MainSub objDIR
    For Each eFolder in objDIR.SubFolders
    GoSubFolders eFolder
    Next
    End If

    End Sub

    Sub MainSub (objDIR)
    For Each efile in objDIR.Files
    WScript.Echo "File ", eFile, "is a ", eFile.Type
    Next
    End Sub


    save it as .vbs extension, type cscript myscript.vbs
blog comments powered by Disqus