# Script to view files and folders on Windows devices

Managing files and folders on devices becomes easier if they are sorted and easily retrievable. Hexnode UEM allows you to view and list all files and folders on Windows devices using [custom scripting](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) feature.

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

 

PowerShell Commands
-------------------

### 1. List of Files and folders

PowerShell script to list of Files and folders

Get-ChildItem -Path\[Specify path\] 

   1

  Get-ChildItem -Path\[Specify path\] 

   

 

  

The **Get-ChildItem** returns a list of all files/folders present in the specified path.

 E.g., Get-ChildItem –Path “C:\\Users\\QA\\Program Files”.

 This command lists all the unhidden files and directories present in the “Program Files” folder.

### 2. List of hidden files and folders

PowerShell script to list hidden files and folders

Get-ChildItem –Path\[Specify path\] -Force 

   1

  Get-ChildItem –Path\[Specify path\] -Force 

   

 

  

The **–Force** parameter returns a list of all files/folders present in the specified path, including the hidden files.

 E.g., Get-ChildItem –Path‘C:\\User\\QA’ -Force.

 This command returns all the files and directories in the folder “QA”, including hidden files, if any.

### 3. List of all folders, sub-folders, files in a folder

PowerShell script to list all folders, sub-folders, files in a folder

Get-ChildItem –Path\[Specify path\] -Recurse -Depth\[Specify number\] 

   1

  Get-ChildItem –Path\[Specify path\] -Recurse -Depth\[Specify number\] 

   

 

  

The **Get-ChildItem -Recurse** returns a list of all files and directories present in the specified folder. The **–Recurse** parameter does not return hidden files. Use the **-Depth** parameter to control the level to which subfolders must be displayed.

 E.g., Get-ChildItem –Path’C:\\User\\QA’-Recurse -Depth 1

 As the **–Depth** parameter is 1, the command will display the content of the parent folder (QA) and immediate subfolders.

 Note:- The output produced with the **–Recurse** parameter is time-consuming if the **-Depth** parameter is not used.

 

### 4. List of all empty files

PowerShell script to list empty files

Get-ChildItem -Path \[Specify path\] -Recurse -File | foreach{ if((Get-Content $\_.FullName) -eq $null){ $\_.FullName }}

   1

2

3

4

  Get-ChildItem -Path \[Specify path\] -Recurse -File | foreach{

if((Get-Content $\_.FullName) -eq $null){

$\_.FullName

}}

   

 

  

The script mentioned above fetches all the files from the specified location. The `Get-Content` cmdlet scans the contents of each file and lists the empty files.

You can also fetch the list of empty files using `length` parameter. It counts the length of the file. If it’s 0, the file is considered empty and displayed in the output.

PowerShell script to list empty files

Get-ChildItem –Path \[Specify path\] -Recurse | where{$\_.Length -eq 0} | Select @{N='EmptyFiles';E={$\_.FullName}} }}

   1

2

3

  Get-ChildItem –Path \[Specify path\] -Recurse | where{$\_.Length -eq 0} |

Select @{N='EmptyFiles';E={$\_.FullName}}

}}

   

 

  

Batch Commands
--------------

### 1. List of all files and folders

Batch script to list all files and folders.

dir \[drive:\]\[path\]\[filename\]\[/q\]\[/w\]\[/d\]\[/a\[\[:\]<attributes>\]\]\[/o\[\[:\]sortorder\]\]\[/t\[\[:\]timefield\]\]\[/s\]\[/b\]\[/l\]\[/n\]\[/x\]\[/c\]\[/4\] 

   1

  dir \[drive:\]\[path\]\[filename\]\[/q\]\[/w\]\[/d\]\[/a\[\[:\]<attributes>\]\]\[/o\[\[:\]sortorder\]\]\[/t\[\[:\]timefield\]\]\[/s\]\[/b\]\[/l\]\[/n\]\[/x\]\[/c\]\[/4\] 

   

 

 Parameters

 ParameterDescription\[drive:\]\[path\]Specify the drive and path of the folder.\[filename\]Specify filename./qSpecify file ownership. E.g., dir “C:\\Users\\QA\\Program files” /q

/wDisplay output in wide format, as many as five file names or directory names on each line. E.g., dir “C:\\Users\\QA\\Program files” /w

/dDisplay output with column-based sorting. E.g., dir “C:\\Users\\QA\\Program files” /d

/a\[\[:\]attributes\]Display the names of directories files according to the specified attributes. - d – Directories
- h – Hidden files
- s – System files
- l – Reparse points
- r – Read-only files
- a – Files ready for archiving
- i – Not content indexed files

Use a colon (:) as a separator, and use hyphen (-) as a prefix to mean “not”. For example, the **-r** attribute won’t show the read-only files.
if a parameter is used without specifying any attributes, the command displays the names of all files, including hidden and system files.

E.g., dir “C:\\Users\\QA\\Program files” /a:h

/o\[\[:\]sortorder\]Display output according to **sortorder** : - n – Alphabetically by name
- e – Alphabetically by extension
- g – Group directories first
- s – By size, smallest first
- d – By date/time, oldest first

Use “-” prefix to reverse the sort order.Use colon( : ) to separate multiple values. If the sort order isn’t specified, **dir /o** lists the directories alphabetically, followed by the files, which are also sorted alphabetically.

E.g., dir “C:\\Users\\QA\\Program files” /o:n

/t\[\[:\]time field\] Specify which time field to display or to use for sorting. The available time field values are: - c – Creation
- a – Last accessed
- w – Last written

E.g., dir “C:\\Users\\QA\\Program files” /t:c

/s Specify all the occurrences of the file name within the directories & subdirectories in the provided path. E.g., dir “C:\\Users\\QA\\Program files\\sys.log” /s

/bDisplay the name of directories and files in the form of a list. No added information such as its read or write permissions or created time will be displayed along with the filename. The **/b** parameter overrides **/w**. E.g., dir “C:\\Users\\QA\\Program files” /b

/l Produce unsorted lowercase directory and file names. E.g., dir “C:\\Users\\QA\\Program files” /l

/n Produce a long list format of all files and folders present in the path. E.g., dir “C:\\Users\\QA\\Program files” /n

/x Display short names generated for non-8dot3 files. E.g., dir “C:\\Users\\QA\\Program files” /x

/c Display a thousand separators in file sizes. This is the default behavior. Use **/-c** to hide separators. E.g., dir “C:\\Users\\QA\\Program files” /c

/4 Display year in 4-digit format. E.g., dir “C:\\Users\\QA\\Program files” /4

 

 

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode UEM will not be responsible for any damage/loss to the system on the behaviour of the script.