Create Files and Folders

SOTI MobiControl
Windows

Creating a file or folder on a single device is easy, but doing so across multiple devices in a specific location can be time-consuming. With SOTI MobiControl, admins can streamline this process by deploying scripts to Windows devices.

Important:
  • It is recommended to test the script on a local/ test machine for its purpose and effects. 
  • SOTI MobiControl will not be responsible for any damage/loss to the data/setup based on the behavior of the script.

Create a folder at the specified path

1 New-Item -Path "<path>" -Name "<foldername>" -ItemType "directory"

Create folder after validating the folder name

1 function IsValidFolderName {
2 param (
3 [string]$FolderName
4 )
5 $invalidChars = '\\', '/', ':', '*', '?', '"', '<', '>', '|'
6 foreach ($char in $invalidChars) {
7 if ($FolderName.Contains($char)) {
8 return $false
9 }
10 }
11 return $true
12 }
13 try {
14 $folderName = "<foldername>"
15 $desktopPath = "<path>"
16 $fullPath = Join-Path -Path $desktopPath -ChildPath $folderName
17 if (-not (IsValidFolderName $folderName)) {
18 throw "Folder name contains invalid characters. It cannot contain any of the following characters: \ / : * ? `" `" `< `>` `|"
19 }
20 if (-not (Test-Path -Path $fullPath -PathType Container)) {
21 New-Item -Path $fullPath -ItemType Directory | Out-Null
22 Write-Output "Folder '$folderName' created successfully on the desktop."
23 } else {
24 Write-Output"Folder '$folderName' already exists on the desktop."
25 }
26 } catch {
27 Write-Output "Error: $_"
28 }

Create a desktop shortcut for the specified folder

1 # Specify the path of the folder you want to create a shortcut for
2 $targetFolder = "C:\Path\To\Your\Folder"
3 # Create a Shell object to access Windows Shell
4 $shell = New-Object -ComObject WScript.Shell
5 # Get the special folder path for the Desktop
6 $desktopPath = "C:\Users\<username>\Desktop"
7 # Create a shortcut object
8 $shortcut = $shell.CreateShortcut("$desktopPath\TestShortcut.lnk")
9 # Set the shortcut properties
10 $shortcut.TargetPath = $targetFolder
11 $shortcut.WindowStyle = 1 # Normal window
12 $shortcut.IconLocation = "explorer.exe,0" # Icon for the shortcut
13 $shortcut.Save() # Save the shortcut
14 Write-Host "Shortcut to folder created on the desktop."

Create a new file at the specified path

1 New-Item <path>\pc.txt

Check if a file exists in a directory and create it if missing

1 #create array of text files
2 $files=Get-ChildItem <path>\*.txt | select -expand fullname
3 #check if file exists inside the array $files -match "<filename>"
4 if($files -match "<filename>")
5 {
6 Write-Output “File Exists”
7 }
8 else {
9 New-Item <path>\<filename>
10 }

Was this helpful?

Need more help?
Ask Community