Search All drives

Hi All,

Been a while since I posted here. I’m trying to do a search for a file. Instead of getting only results from the C: drive I’m trying to include usb and mapped drives as well. So far I’ve only gotten get-childitem -Path C:\ -Filter *$searchedobject*. Problem is I can’t figure out how to include any additional drives. Thoughts?

Thanks,

Have you tried to search for a solution? :thinking: :man_shrugging:

In the simplest case you can use Get-PSDrive to list all available drives. And to narrow it down to the ones with filesystems on it you can use Get-PSDrive -PSProvider 'FileSystem'.

1 Like

Get-PSDrive -PSProvider FileSystem | ForEach-Object {Get-ChildItem $_.Root -Filter *.txt -Recurse}

To get all logical disk, you can use

get-ciminstance -class win32_logicaldisk | foreach { Get-ChildItem "$($_.Name)\" -Filter *.txt -Recurse }

If you want to scan only local disk, you can add a where clause.

get-ciminstance -class win32_logicaldisk | where { $_.DriveType -eq 3 } | foreach { Get-ChildItem "$($_.Name)\" -Filter *.txt -Recurse }

If you want to scan only network drive, you can change the where clause.

get-ciminstance -class win32_logicaldisk | where { $_.DriveType -eq 4 } | foreach { Get-ChildItem "$($_.Name)\" -Filter *.txt -Recurse }

An other solution to get logical disk is :

get-ciminstance -query "select * from win32_logicaldisk where DriveType -eq 4" | foreach { Get-ChildItem "$($_.Name)\" -Filter *.txt -Recurse }

The difference is

  1. in the first solution (-class…), WMI returns all results and the pipeline applies the filter with where
  2. in the second one (-query), WMI returns directly filtered results.

With logicaldisk, this is not significant but for others classes with many instances, it is more optimized to use -query.

Example on my Windows 10 Workstation :

$arrServices = get-ciminstance -query "select * from win32_service"
write-host "count : $($arrServices.count)
353

$arrServices = get-ciminstance -query "select * from win32_service name like 'win%'"
write-host "count : $($arrServices.count)
4

Thanks for this info. I’m going to try all this for my script.