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
}