Public/New-SpecFolder.ps1

function New-SpecFolder {
    <#
    .SYNOPSIS
    Creates a new folder at the specified path if it does not already exist.
 
    .DESCRIPTION
    The New-SpecFolder function creates a new folder at the specified path if it does not already exist.
    If the folder already exists, it provides a message indicating that the folder is already present (using write-verbose).
 
    .PARAMETER Path
    Specifies the path where the new folder should be created. It accepts input from pipeline or direct parameter input.
 
    .EXAMPLE
    "C:\ExampleFolder", "C:\AnotherFolder" | New-SpecFolder
    Creates new folders at the paths "C:\ExampleFolder" and "C:\AnotherFolder" sent through the pipeline.
 
    .EXAMPLE
    New-SpecFolder -Path "C:\ExampleFolder", "C:\AnotherFolder"
    Creates new folders at the paths "C:\ExampleFolder" and "C:\AnotherFolder" provided via the -Path parameter.
 
    .INPUTS
    System.String[]
 
    .OUTPUTS
    System.Int32
    Returns 0 if the folders are created successfully.
    Returns 1 if there is an error while creating any of the folders.
    Returns 2 if any of the folders already exist.
 
    .NOTES
    Author : owen.heaume
    Version : 2.0
    #>


    [cmdletbinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string[]]$Path
    )

    process {
        # Process each input path
        foreach ($p in $Path) {
            # Check if the folder already exists, and if not, create it
            #if (![System.IO.Directory]::Exists($p)) { # faster than test path - impossible to unit test!
            if (!(Test-Path -Path $p -PathType Container)) {
                try {
                    Write-Verbose "Creating folder '$p'"
                    $null = New-Item -ItemType Directory -Path $p -ErrorAction Stop
                    Write-Verbose "Folder '$p' created"
                    return 0
                } catch {
                    Write-Error "Failed to create folder '$p': $_"
                    return 1
                }
            } else {
                Write-Verbose "Folder '$p' already exists"
                return 2
            }
        }
    }
}