src/Utilities/Add-XrmFolder.ps1

<#
    .SYNOPSIS
    Add folder in given path if it doesn't exists.

    .DESCRIPTION
    Create given folder if not exist and return sub folder full path.

    .PARAMETER Path
    Folder path where to add given folder. A relative path is read from the PowerShell location.

    .PARAMETER FolderName
    Folder name.

    .PARAMETER CleanIfExists
    If folder already exists, remove existing content. (Default : False)

    .OUTPUTS
    System.String. Full path of the folder.

    .EXAMPLE
    $exportFolderPath = Add-XrmFolder -Path "C:\Temp" -FolderName "Solutions" -CleanIfExists;

    .LINK
    https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/Add-XrmFolder.md
#>

function Add-XrmFolder {
    [CmdletBinding()]
    [OutputType([String])]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateScript( { Test-Path $_ })]
        [String]
        $Path,

        [Parameter(Mandatory = $true)]
        [String]
        $FolderName,

        [Switch]
        $CleanIfExists
    )
    begin {  
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {
        # Relative to the PowerShell location, not to the folder of the process (M25)
        if ($Path) { $Path = Resolve-XrmPathInternal -Path $Path; }
        $folderPath = [System.IO.Path]::Combine($Path, $FolderName);
        if ($CleanIfExists) {
            if (Test-Path $folderPath) {
                Remove-Item -Recurse -Force $folderPath;
            }
        }
        New-Item -ItemType Directory -Force -Path $folderPath | Out-Null;
        
        $folderPath;
    }
    end {
        $StopWatch.Stop();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }
}

Export-ModuleMember -Function Add-XrmFolder -Alias *;