ConvertFrom-PowerShellDataFile.psm1

#───────────────────────────────────────────────────────────────────────────────────────
# Module Name: ConvertFrom-PowerShellDataFile
# Version: 1.1.4
# Author: Nathaniel Wallis Praytor
#───────────────────────────────────────────────────────────────────────────────────────
function ConvertFrom-PowerShellDataFile {
    <#
        .SYNOPSIS
            Constructs a System.Collections.Hashtable Object from the contents stored in a specified PowerShell Data File (.psd1 extension).
 
        .DESCRIPTION
            Constructs a Custom Object of the TypeName 'System.Collections.Hashtable' around/from the contents stored within a specified PowerShell Data File/Module Manifest (.psd1 extension).
            It accomplishes this by the following means:
            First, an Object with the TypeName 'System.Collections.Hashtable' is created using the built-in 'New-Object' Cmdlet, and the resulting Object is immediately stored in the $Manifest Variable. Next, the 'Import-PowerShellDataFile' Cmdlet is used to load the files contents into directly into the Object via the Variable it was stored in, followed by another Variable being initialized and being set a value equal to the files BaseName using 'Split-Path $Filename -Leaf'. Lastly, the final Output variable is created, this time using the 'New-Variable' Cmdlet, and passing the $BaseName Variable to its 'Name' Parameter, and the $Manifest Variable is passed to its 'Value' Parameter.
 
        .PARAMETER FilePath
            Specifies the Path to a PowerShell Data File.
 
        .EXAMPLE
            PS C:\> ConvertFrom-PowerShellDataFile -FilePath 'C:\test\MyModule\PSScriptAnalyzerSettings.psd1'
            (The file 'PSScriptAnalyzerSettings.psd1' is converted to a Hashtable and stored in a Variable called '$PSScriptAnalyzerSettings')
 
        .LINK
            https://github.com/SupernautFX/ConvertFrom-PowerShellDataFile
            https://github.com/SupernautFX/SFX.PoshUtilities.DataFileTools
            https://github.com/SupernautFX/Posh-Configuration-Tool
 
        .NOTES
            Filename: 'ConvertFrom-PowerShellDataFile.ps1'
            Version: 1.1.2
            Author: Nathaniel Wallis Praytor
 
        .FORWARDHELPTARGETNAME ConvertFrom-PowerShellDataFile
        .FORWARDHELPCATEGORY Function
        .EXTERNALHELP ConvertFrom-PowerShellDataFile.psm1-help.xml
    #>


    [CmdletBinding(SupportsShouldProcess)]
    [OutputType([System.Collections.Hashtable])]
    param(
        [Parameter(
            Mandatory,
            Position = 0,
            ValueFromPipeline,
            ValueFromPipelineByPropertyName,
            HelpMessage = 'Specifies the path to a PowerShell Data File.'
        )]
        [Alias('Path')]
        [string]$FilePath
    )

    BEGIN {
        Write-Information ""
    }

    PROCESS {
        Try {
            $Manifest = New-Object hashtable
            $Filename = Split-Path $FilePath -Leaf
            $BaseName = $Filename.Split('.')[0]
            $Manifest = (Import-PowerShellDataFile -Path $FilePath)
            New-Variable -Name $BaseName -Value $Manifest
        } Catch {
            Write-Error $_
        }
    }

    END {
        
    }
}

Set-Alias -Name cfpsd1 -Value ConvertFrom-PowerShellDataFile

Set-Alias -Name ConvertFrom-PSD1 -Value ConvertFrom-PowerShellDataFile

Export-ModuleMember -Function ConvertFrom-PowerShellDataFile

Export-ModuleMember -Alias cfpsd1 -Function ConvertFrom-PowerShellDataFile
Export-ModuleMember -Alias ConvertFrom-PSD1 -Function ConvertFrom-PowerShellDataFile