Private/New-FolderStructure.ps1
Function New-FolderStructure { <# .SYNOPSIS Creates a basic folder structure with a root folder and a "src" subfolder. .DESCRIPTION This function automates the creation of a standard folder structure for organizing project files. It includes a root folder (defined by the function's parameters) and a "src" subfolder within it. The function will indicate if folders or subfolders already exist. .PARAMETER Path Required. Specifies the absolute path where the root folder will be created. .PARAMETER Name Required. The name of the root folder. .EXAMPLE New-FolderStructure -Path C:\MyProjects -Name NewProject The above command creates a folder named "NewProject" at the location "C:\MyProjects" and also creates a subfolder named "src" inside it. .NOTES Author: owen.heaume Version: 1.0.0 - Initial release #> [cmdletbinding()] Param( [Parameter(Mandatory)] [string]$Path, [Parameter(Mandatory)] [string]$Name ) Begin { Write-Host "Creating folder structure" -ForegroundColor DarkCyan try { $folderPath = Join-Path -Path $Path -ChildPath $name -ea Stop } catch { throw "Error joining path: $_" } } Process { # Create the root folder if (-not (Test-Path $folderPath)) { Write-Host "Creating folder: $folderPath" -ForegroundColor DarkGray try { New-Item -Path $folderPath -ItemType Directory -ea stop | Out-Null write-host "Folder created successfully" -ForegroundColor DarkGreen } catch { throw "Error creating folder: $_" } } else { Write-Host "$folderPath already exists" -ForegroundColor DarkYellow } # Create the subfolder 'src' if (-not (Test-Path (Join-Path -Path $folderPath -ChildPath "src"))) { Write-Host "Creating subfolder: $(Join-Path -Path $folderPath -ChildPath "src")" -ForegroundColor DarkGray try { New-Item -Path (Join-Path -Path $folderPath -ChildPath "src") -ItemType Directory -ea stop | Out-Null write-host "Subfolder created successfully" -ForegroundColor DarkGreen } catch { throw "Error creating subfolder: $_" } } else { Write-Host "$(Join-Path -Path $folderPath -ChildPath "src") already exists" -ForegroundColor DarkYellow } [pscustomobject]@{ FolderPath = $folderPath Src = (Join-Path -Path $folderPath -ChildPath "src") } } End {} } |