CmdLets/Master/New-ItemWrapper.ps1
|
Function New-ItemWrapper { <# .SYNOPSIS A wrapper function that recursively creates items (directories or registry keys) and their missing parents. .DESCRIPTION The New-ItemWrapper function extends standard item creation by automatically checking for and recursively creating any missing parent paths (directories or registry keys) before creating the target item. It detects the appropriate PSDriver provider (such as FileSystem or Registry) based on the path format and invokes New-Item with the correct settings. .PARAMETER Path The target path (file system directory path or registry path) to be created. .EXAMPLE New-ItemWrapper -Path "C:\Tools\Scripts\Logs" Description: Recursively creates the "Tools", "Scripts", and "Logs" directories if any of them do not already exist. .NOTES Prerequisites: - Uses recursion to evaluate and build parent paths from the root upward. .LINK https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item #> [CmdletBinding()] Param( [Parameter(Mandatory=$True)][String]$Path ) $Parent = Split-Path $Path -Parent If (!(Test-path $Parent)) { New-ItemWrapper -Path $Parent } if ($Path -like "*:*"){ $Drive = $Path.Substring(0,$Path.Indexof(":")) $Provider = (Get-PSDrive -Name $Drive).Provider.Name } elseif ($Path -like "\\*") { $Provider = "FileSystem" } Switch ($Provider) { "Registry" { New-Item $Path | Out-Null } "FileSystem" { New-Item $Path -ItemType Directory | Out-Null } Default { New-Item $Path | Out-Null } } } |