Script to search for files with listed extension RRS feed

  • Question

  • I have created a powershell script to search for files with a certain extension.

    ---start code---

    # Import the AD module to the session
    Import-Module ActiveDirectory

    $computers = Get-ADComputer -Filter * -SearchBase "OU=Servers, DC=domain, DC=com" -Properties dNSHostName |Select-Object -ExpandProperty dNSHostName

    $filenames = Get-Content "C:\bin\scripts\filenamelist.txt"

    foreach ($computer in $computers) {

    foreach ($filename in $filenames) {
    Write-host $computer $filename
    Get-ChildItem -Recurse -Force \\$computer\c$, \\$computer\d$, \\$computer\e$, \\$computer\f$, \\$computer\g$,\\$computer\h$ -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and  ( $_.Name -like "$filename") } | Select-Object Directory,Name| Export-Csv C:\temp\FilesFound.csv -nti -append

    ---end code---

    You will require a file called "filenamelist.txt" where you enter each extension on a new line.

    The script works well however there are some things I like to better.  Maybe someone can help to do that.

    - Currently the script takes ages to complete when you search for files on different drives on a lot of servers.

    => it would be nice if the script could do an auto discovery of all drive letters of each server, instead of specifying drive c: d: e: f: etc..

    => it should be possible to run different threads to shorten the scanning time (now for 88 servers it takes over a week)

    => you should be able to enter exceptions, eg don't scan c:\windows\system32

    Thanks for reading and your input.


    • Edited by mrbbs86 Wednesday, December 28, 2016 1:31 PM
    Wednesday, December 28, 2016 11:38 AM


  • the following will give you the physical drives and Iscsi drives on a physical server, plus size and free space and volume name.

    get-wmiobject -Class Win32_logicaldisk -filter "drivetype=3" -computer $computer

    Wednesday, December 28, 2016 1:01 PM
  • IF you use WMI and Cim_DataFIle class you can search all drives in one line.  You can also run jobs so each computer runs concurrently.  Start by learning about Get-WMIObject and jobs as well as how to write and use WMI with PowerShell.

    Look in the Gallery for many examples.


    Wednesday, December 28, 2016 6:50 PM

All replies