Public/Set-PsModuleBuilderPath.ps1
function Set-PsModuleBuilderPath { <# .Synopsis Short description .DESCRIPTION Long description .EXAMPLE Example of how to use this cmdlet #> [CmdletBinding(SupportsShouldProcess=$true)] param ( [Parameter( Mandatory=$false, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [string]$Path="", [Alias("Default")] [switch]$ShouldUseDefault ) BEGIN{ $dataPath = "$PsScriptRoot\..\Data\psmodulebuilderpath.xml" $errorMessage = @" Path provided is not a valid container path... To set the default module path run: PS> Set-PsModuleBuilderPath -Default "@ $whatifMessage = $Path if (Confirm-IsInvalid $Path) { $whatifMessage = $errorMessage } }#begin PROCESS{ if ($psCmdlet.ShouldProcess("Setting new path", $whatifMessage)) { if ($ShouldUseDefault){ $defaultPath = Get-UserPsModulePath Write-Debug "Setting default path: $defaultPath" $defaultPath | Export-CliXml -Path $dataPath } else { if (Confirm-IsInvalid $Path) { throw $errorMessage } Write-Debug "Setting path: $Path" $Path | Export-CliXml -Path $dataPath } } }#process END{ }#end } function Get-UserPsModulePath { $modulesOneDriveBackup = "$env:USERPROFILE\OneDrive\Documents\WindowsPowerShell\Modules" if (Test-Path $modulesOneDriveBackup -PathType Container) { return $modulesOneDriveBackup } $userModulesPath = "$env:USERPROFILE\Documents\WindowsPowerShell\Modules" if (-not (Test-Path $userModulesPath -PathType Container)) { New-Item -Path $userModulesPath -ItemType Directory -Force | Out-Null } return $userModulesPath } function Confirm-IsInvalid { param([string]$Path) $isEmpty = [string]::IsNullOrEmpty($Path) if ($isEmpty) { return $true } $isNotContainer = !(Test-Path $Path -PathType Container) return $isNotContainer } |