# Script to create a new folder if it doesn’t exist on Windows

A system admin might need to create new folders on employee devices to keep things organized. Doing this manually on each device would be a hassle, especially if the company has numerous endpoints deployed across the globe. This doc assists you on how to remotely create a new folder on Windows devices if it doesn’t already exist by [executing custom scripts](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) via Hexnode UEM.

 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.

 

Batch Script
------------

Sample script

@echo off cls if not exist "enter\_folder\_path" ( mkdir "enter\_folder\_path" echo Folder created. ) else ( echo Folder already exists! ) 

   1

2

3

4

5

6

7

8

9

  @echo off 

cls 

if not exist "enter\_folder\_path" (

mkdir "enter\_folder\_path"

echo Folder created.

 ) else (

echo Folder already exists!

) 

   

 

  

**For example:** To create a folder named ‘FolderA’ in the Desktop of the user Deborah, replace enter\_folder\_path with *C:\\Users\\Deborah\\Desktop\\FolderA\\*

Remember to put a backslash after the folder name; else, it will search for a file rather than a folder.

PowerShell Script
-----------------

Sample script

$folderName = "enter\_folder\_name\_here" $Path = "enter\_folder\_path" + $folderName if (!(Test-Path $Path)) { New-Item -ItemType Directory -Path $Path -Force Write-Output "Folder created." } else { Write-Output "Folder already exists!" }

   1

2

3

4

5

6

7

8

9

10

  $folderName = "enter\_folder\_name\_here"

$Path = "enter\_folder\_path" + $folderName

if (!(Test-Path $Path)) {

 New-Item -ItemType Directory -Path $Path -Force

 Write-Output "Folder created."

}

else {

 Write-Output "Folder already exists!"

}

   

 

  

![Script to create a folder if it doesn’t exist on Windows remotely executed successfully from Hexnode.](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2022/01/Folder-created-on-Windows-remotely-with-script--300x148.png)

To create a folder named ‘FolderA’ in the Desktop of the user Deborah, replace enter\_folder\_name\_here with *FolderA* and replace enter\_folder\_path with *C:\\Users\\Deborah\\*

If you want to create a folder with the folder name as that particular day’s date, replace enter\_folder\_name\_here with *(Get-Date).tostring(“dd-MM-yyyy”)* without the double-inverted commas.

i.e., $folderName = (Get-Date).tostring(“dd-MM-yyyy”)

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:- 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.