FileSystemHelpers.psm1
function GetTempPath { [System.IO.Path]::GetTempPath() } function Get-TempPath { $tempPath = GetTempPath if ([String]::IsNullOrEmpty($tempPath)) { throw [System.IO.FileNotFoundException]::new('The temporary path returned by [System.IO.Path]::GetTempPath() is empty.') } $tempPath } function New-TempDirectory { [CmdletBinding(SupportsShouldProcess)] param () $tempPath = New-TempPath if ($PSCmdlet.ShouldProcess($tempPath)) { New-Item -Path $tempPath -ItemType 'Directory' -WhatIf:$WhatIfPreference } } function New-TempFile { [CmdletBinding(SupportsShouldProcess)] param ( [string]$Extension ) $tempPath = New-TempPath -Extension $Extension if ($PSCmdlet.ShouldProcess($tempPath)) { New-Item -Path $tempPath -ItemType 'File' -WhatIf:$WhatIfPreference } } function New-TempPath { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] param ( [string]$Extension ) do { if ([System.String]::IsNullOrWhiteSpace($Extension)) { $name = (New-Guid).Guid } else { $name = '{0}.{1}' -f (New-Guid).Guid, $Extension } $tempPath = [System.IO.Path]::Combine((Get-TempPath), $name) } while (Test-Path -Path $tempPath) $tempPath } |