# Script to find a File on Windows devices

Searching for specific files on a device can be tedious and time consuming for users. If searching for and finding files are made easy, managing data becomes easier. Hexnode lets admins find files on devices with specific extensions using [scripts](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) .

 Note: 
**Supported Versions**:

The scripts given below will be supported on the following versions:

- Windows 10 v1607+ (Pro, Enterprise, Education)
- Windows 11 (Pro, Enterprise, Education)

[ ](https://developer.android.com/develop/connectivity/uwb#uwb-enabled_mobile_devices)

 

 Disclaimer:The sample scripts provided below are adapted from third-party open-source sites.

 

Find files using Batch script
-----------------------------

Sample bash script to find a file on Windows

@echo off REM Set the filename (include extension) set "FileName=Enter file name" REM For example: "sample.jpg" REM Set the directory to search set "SearchDir=Enter path to search" REM For example: "C:\\" REM Flag to track if file is found set "Found=" REM Search recursively for the file for /r "%SearchDir%" %%f in (\*%FileName%) do ( echo File "%FileName%" is located at: %%f set "Found=1" ) REM If not found, show message if not defined Found ( echo File "%FileName%" not found in %SearchDir% exit /b 1 ) exit /b 0

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

  @echo off

REM Set the filename (include extension)

set "FileName=Enter file name"

REM For example: "sample.jpg"

REM Set the directory to search

set "SearchDir=Enter path to search"

REM For example: "C:\\"

REM Flag to track if file is found

set "Found="

REM Search recursively for the file

for /r "%SearchDir%" %%f in (\*%FileName%) do (

 echo File "%FileName%" is located at: %%f

 set "Found=1"

)

REM If not found, show message

if not defined Found (

 echo File "%FileName%" not found in %SearchDir%

 exit /b 1

)

exit /b 0

   

 

  

![Batch script to find files on a Windows device executed successfully from Hexnode.](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2021/12/Windows-file-found-remotely-with-Batch-script--300x149.png)

Provide the filename with extension and the directory path where you want to search for the file in the placeholders ***“set FileName”*** and ***“set SearchDir”***.

Find files using PowerShell script
----------------------------------

Sample Powershell script to find a file on Windows

\# Set the filename explicitly (include extension) $FileName = "Enter file name" # For example: "sample.txt" # Directory to search $SearchDir = "Enter path to search" # For example: "C:\\" # Find matching files and output full paths $matches = Get-ChildItem -Path $SearchDir -Recurse -File -Filter $FileName -ErrorAction SilentlyContinue if ($matches) { foreach ($match in $matches) { Write-Output "File '$FileName' is located at: $($match.FullName)" } } else { Write-Output "File '$FileName' not found in $SearchDir" exit 1 }

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

  \# Set the filename explicitly (include extension)

$FileName = "Enter file name" \# For example: "sample.txt"

\# Directory to search

$SearchDir = "Enter path to search" \# For example: "C:\\"

\# Find matching files and output full paths

$matches = Get-ChildItem -Path $SearchDir -Recurse -File -Filter $FileName -ErrorAction SilentlyContinue

if ($matches) {

 foreach ($match in $matches) {

 Write-Output "File '$FileName' is located at: $($match.FullName)"

 }

} else {

 Write-Output "File '$FileName' not found in $SearchDir"

 exit 1

}

   

 

  

![PowerShell script to find files on a Windows device executed successfully from Hexnode.](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2021/12/Windows-file-found-remotely-with-PowerShell-script--300x149.png)

Provide the filename with extension and the directory path where you want to search for the file in the placeholders ***“$FileName”*** and ***“$SearchDir”***.

How to View Script Output in Hexnode
------------------------------------

To review the execution results, navigate to the **Action History** tab of the specific device in your Hexnode UEM portal. Locate the script entry in the **Subject** column and click the **Show Output** button next to the status field to view the returned data.

 Notes:- The latest version of the Hexnode Agent app should be installed on the device.
- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.